None Ch2_exe

Table of Contents

In [138]:
print(R.version)
file.path(R.home("bin"), "R")
               _                           
platform       x86_64-apple-darwin13.4.0   
arch           x86_64                      
os             darwin13.4.0                
system         x86_64, darwin13.4.0        
status                                     
major          4                           
minor          3.1                         
year           2023                        
month          06                          
day            16                          
svn rev        84548                       
language       R                           
version.string R version 4.3.1 (2023-06-16)
nickname       Beagle Scouts               
'/Users/karlzhang/miniforge3/envs/JTR/lib/R/bin/R'
In [1]:
library(IRdisplay) # display
library(fUnitRoots) # adfTest
library(TSA) # eacf
Attaching package: ‘TSA’


The following objects are masked from ‘package:stats’:

    acf, arima


The following object is masked from ‘package:utils’:

    tar


In [511]:
# rm(plot_arima_forecast_fig)
In [631]:
# detach("package:AFTSCode", unload = TRUE) # First detach the package
# unloadNamespace("AFTSCode")
In [632]:
library(AFTSCode)
Attaching package: ‘AFTSCode’


The following object is masked _by_ ‘.GlobalEnv’:

    deep_copy


2-1

  • From Sec. 2.5.4
    • $E[R_{101}|F_{100}] = c_0 - \theta_1 a_{100} = 0.2*0.01 = 0.002$
    • $Var[R_{101}|F_{100}] = \sigma_{a}^2 = 0.025^2 = 0.000625$
    • $E[R_{102}|F_{100}] = c_0 = 0$
    • $Var[R_{102}|F_{100}] = (1+\theta_1^2)\sigma_{a}^2 = (1+0.2^2)*0.025^2 = 0.00065$
  • From Sec. 2.5.1
    • $\rho_1 = -(-0.2)/(1+0.2^2) = 0.192307692307692$
    • $\rho_2 = 0$

2-2

  • From Sec. 2.4.1
    • $E[r_t]=\frac{\phi_0}{1-\phi_1}=0.01/(1-0.2)=0.0125$
    • $Var[r_t]=\frac{\sigma_a^2}{1-\phi_1^2}=0.02/(1-0.2^2)=0.0208333333333333$
  • From Sec. 2.4.2
    • $\rho_0=1$
    • $\rho_1=\phi_1\rho_0=0.2$
    • $\rho_2=\phi_1\rho_1=0.04$
  • From Sec. 2.4.4
    • $E[r_{101}|F_{100}]=\phi_0+\phi_1*r_{100}=0.01+0.2*(-0.01)=0.008$
    • $Var[r_{101}|F_{100}]=\sigma_a^2=0.02$
    • $E[r_{102}|F_{100}]=\phi_0+\phi_1E[r_{101}|F_{100}]=0.01+0.2*0.008=0.0116$
    • $Var[r_{102}|F_{100}]=Var[\phi_1 r_{101}+a_{102}|F_{100}]=Var[\phi_1 (\phi_0+\phi_1 r_{100}+a_{101})+a_{102}|F_{100}]=(1+\phi_1^2)\sigma_a^2=(1+0.2^2)*0.02=0.0208$

2-3 (Monthly Unemployment Rate)

  • Interesting observations.
    • The diff(log(unem_rate)) shows first strong momentum, second strong mean-reverting, and serial correlation in pacf. See plot_pacf_acf results.
In [554]:
da = read.table("../AFTS_sol/data/m-unrate.txt", header = TRUE)
da[1:5,]
A data.frame: 5 × 4
YearMonDayRate
<int><int><int><dbl>
11948113.4
21948213.8
31948314.0
41948413.9
51948513.5
In [555]:
unem_rate = da$Rate
lg_unem_rate = log(unem_rate)
unem_rate_ts = ts(unem_rate, frequency = 12, start = c(1948,1))
lg_unem_rate_ts = ts(lg_unem_rate, frequency = 12, start = c(1948,1))
In [143]:
plot_time_fig(unem_rate_ts, main = "Unemployment Rate", xlab = "Year")
pacf(unem_rate)
No description has been provided for this image
No description has been provided for this image
In [22]:
plot_time_fig(lg_unem_rate_ts, main = "log(Unemployment Rate)", xlab = "Year")
pacf(lg_unem_rate)
No description has been provided for this image
No description has been provided for this image
In [557]:
plot_pacf_acf(lg_unem_rate, freq = 12, lag.max = 60)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image

Unit-root test

  • We can reject the null-hypothesis from ADF test. In other words, the process doesn't have unit-root.
  • Seems like we cannot reject he null hypothesis when we use large enough lags in the ADF testing.
  • Also from the ACF plots of the unemployment rate before and after taking difference, we can see that we need to take difference to cancel the strong serial correlation.
  • From the ACF plot of the unemployment rate after taking difference, we see some damping sine and cosine wave, indicating that there could be some business cycle (pp42).
In [34]:
# "lags=12" is chosen from PACF plot
adf_test_res = adfTest(unem_rate_ts, lags = 12, type = c("ct"))
adf_test_res
Title:
 Augmented Dickey-Fuller Test

Test Results:
  PARAMETER:
    Lag Order: 12
  STATISTIC:
    Dickey-Fuller: -2.9715
  P VALUE:
    0.1671 

Description:
 Tue Feb 27 23:20:00 2024 by user: 
In [31]:
lag.max = 60
plot_acf(da = unem_rate, lag.max = lag.max, main = "Unemployment Rate")
pacf(unem_rate, lag.max = lag.max)
No description has been provided for this image
No description has been provided for this image
In [ ]:

In [35]:
diff_unem_rate = diff(unem_rate)
diff_unem_rate_ts = ts(diff_unem_rate, frequency = 12, start = c(1948,2))
In [36]:
plot_acf(da = diff_unem_rate, lag.max = lag.max, main = "diff(Unemployment Rate)")
pacf(diff_unem_rate, lag.max = lag.max)
No description has been provided for this image
No description has been provided for this image
In [144]:
plot_acf(da = diff(unem_rate, 12), lag.max = lag.max, main = "diff(Unemployment Rate, 12)")
pacf(diff(unem_rate, 12), lag.max = lag.max)
No description has been provided for this image
No description has been provided for this image

Order determination

  • Try to use ARIMA(4, 1, 5) model.
In [172]:
perform_and_print_eacf <- function(da, ar.max, ma.max) {
    eacf_obj <- eacf(da, ar.max = ar.max, ma.max = ma.max)
    eacf_stats_tb = format(as.data.frame(eacf_obj$eacf), digits = 3)
    names(eacf_stats_tb) <- seq(from = 0, to = ma.max)
    display(eacf_stats_tb)
    display(eacf_obj$symbol)
    # pp67, asymptotic standard error of EACF
    display(2/sqrt(length(da)))
    c(eacf_obj, eacf_stats_tb)
}
In [173]:
unem_rate_eacf_res <- perform_and_print_eacf(diff_unem_rate, ar.max = 25, ma.max = 12)
A data.frame: 26 × 13
0123456789101112
<I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>>
1 0.1119 0.29465 0.21842 0.17198 1.94e-01 8.39e-02 0.05184 0.051878 0.02445-0.096187 0.026951-0.1594-0.047376
2-0.3443 0.15698-0.00881-0.03887 1.07e-01-3.19e-02-0.01686 0.022971 0.00774-0.077354-0.024625-0.1687 0.067674
3-0.4642 0.13045-0.01960-0.02493 1.13e-01-4.40e-02-0.06191 0.004640 0.01418-0.067424 0.002018-0.1414 0.040126
4-0.3650-0.35458 0.02198 0.08834 9.75e-02-5.66e-05 0.01257-0.004783 0.01615-0.066850-0.018419-0.1430-0.070736
5-0.4954-0.30539 0.04120 0.30176 9.52e-02-2.57e-03 0.01766 0.009470 0.04559-0.042195 0.047631-0.1245-0.039347
6 0.2517 0.44829 0.07516 0.16677 1.08e-01-8.10e-02-0.01934 0.000521 0.06292 0.022652-0.002533-0.1239-0.036224
7-0.3005 0.35966-0.13538-0.00859 3.02e-01-1.25e-01-0.01774 0.001050 0.05707 0.012168-0.005929-0.1340 0.021891
8-0.3330-0.00552-0.08958-0.02068 1.83e-01-1.10e-01 0.00728-0.032169 0.06110-0.009638 0.022461-0.0940 0.004250
9-0.3266 0.00149-0.33486-0.11095 1.03e-01 7.02e-02 0.24701 0.252584-0.02356-0.027958 0.056793-0.0928-0.000937
10-0.0757-0.16764-0.36087-0.22614 1.28e-01 2.94e-02 0.17613 0.275884-0.10130-0.033224 0.050234-0.0713-0.073394
11 0.2026-0.47690-0.28373 0.31492 1.15e-01-8.30e-02 0.01870 0.213078 0.19785 0.161355-0.028338-0.0807-0.073848
12 0.2286-0.47033-0.25334 0.19384 1.51e-01-1.63e-01 0.04894 0.212287 0.26369 0.145681-0.107458-0.0696 0.024317
13-0.1529 0.17761 0.20052 0.01742-2.30e-02 5.50e-02 0.11660-0.016648-0.10144 0.065135-0.049605-0.1695-0.024814
14 0.4946 0.10012 0.14184 0.13014 5.92e-05 1.60e-02 0.14602-0.014817-0.11259-0.022876-0.003969-0.1224 0.157803
15-0.3646 0.36006 0.08090 0.07057-2.23e-02 9.10e-03 0.01581 0.036602-0.11763 0.003957-0.005981-0.1541 0.218884
16-0.3713 0.26612-0.11562-0.00153 1.90e-02-1.23e-02 0.01686 0.018377-0.00921-0.015470 0.003599-0.1360 0.224311
17 0.3182 0.32908-0.11731-0.01477 8.46e-02-5.94e-02 0.01226-0.026056-0.00161-0.029129-0.007909-0.2376 0.189411
18-0.4914 0.15908-0.17106 0.04494 7.80e-02 1.66e-02 0.02160 0.014086-0.01989-0.000608-0.008598-0.3270-0.057128
19 0.2433 0.23853-0.15809 0.00850 3.95e-02 4.90e-02 0.00793 0.034214-0.02280-0.006053 0.000501-0.3291 0.077020
20-0.5005 0.42667-0.24634 0.00140 1.12e-01-1.23e-02 0.07434 0.057412-0.04946 0.004030 0.000927-0.2956 0.222547
21 0.4127 0.40983 0.09004 0.02077 5.28e-02 1.99e-02 0.06946 0.102419-0.03728-0.033084-0.063026-0.2526 0.120537
22-0.4559 0.18572-0.09269 0.00801 8.55e-02-4.82e-02 0.06485-0.051137 0.11544-0.129892-0.019547-0.2897-0.073481
23-0.4140-0.20397-0.03547 0.17919 1.34e-01-1.95e-01-0.07790 0.058271 0.21587-0.151503 0.016659-0.1912 0.124205
24-0.2072-0.26001-0.03256 0.22710 1.20e-01-1.92e-01-0.06062 0.140398 0.20160-0.156237-0.183888-0.2841 0.194636
25 0.0867 0.14692 0.37815 0.10849 8.49e-02 1.53e-01-0.04019-0.059815-0.11085-0.052183-0.286084-0.3222-0.139143
26-0.4406-0.00685 0.31666-0.11685-2.49e-02 1.73e-01-0.09294-0.008748-0.05978 0.004758-0.021080-0.3346 0.137259
A matrix: 26 × 13 of type chr
0123456789101112
0xxxxxxoooxoxo
1xxooxooooxoxo
2xxooxooooooxo
3xxoxxooooooxo
4xxoxxooooooxo
5xxxxxxoooooxo
6xxxoxxoooooxo
7xoxoxxoooooxo
8xoxxxoxxoooxo
9xxxxxoxxxoooo
10xxxxxxoxxxoxo
11xxxxxxoxxxxoo
12xxxoooxoxooxo
13xxxxooxoxooxx
14xxxoooooxooxx
15xxxooooooooxx
16xxxoxooooooxx
17xxxoxooooooxo
18xxxooooooooxx
19xxxoxooooooxx
20xxxooooxoooxx
21xxxoxoooxxoxo
22xxoxxxxoxxoxx
23xxoxxxoxxxxxx
24xxxxxxooxoxxx
25xoxxoxxooooxx
0.0738213470525562
AR/MA
   0 1 2 3 4 5 6 7 8 9 10 11 12
0  x x x x x x o o o x o  x  o 
1  x x o o x o o o o x o  x  o 
2  x x o o x o o o o o o  x  o 
3  x x o x x o o o o o o  x  o 
4  x x o x x o o o o o o  x  o 
5  x x x x x x o o o o o  x  o 
6  x x x o x x o o o o o  x  o 
7  x o x o x x o o o o o  x  o 
8  x o x x x o x x o o o  x  o 
9  x x x x x o x x x o o  o  o 
10 x x x x x x o x x x o  x  o 
11 x x x x x x o x x x x  o  o 
12 x x x o o o x o x o o  x  o 
13 x x x x o o x o x o o  x  x 
14 x x x o o o o o x o o  x  x 
15 x x x o o o o o o o o  x  x 
16 x x x o x o o o o o o  x  x 
17 x x x o x o o o o o o  x  o 
18 x x x o o o o o o o o  x  x 
19 x x x o x o o o o o o  x  x 
20 x x x o o o o x o o o  x  x 
21 x x x o x o o o x x o  x  o 
22 x x o x x x x o x x o  x  x 
23 x x o x x x o x x x x  x  x 
24 x x x x x x o o x o x  x  x 
25 x o x x o x x o o o o  x  x 

Fit an ARIMA(3,1,5) model

In [108]:
unem_rate_mod = stats::arima(unem_rate_ts, order = c(3,1,5), include.mean = F)
unem_rate_mod
Call:
stats::arima(x = unem_rate_ts, order = c(3, 1, 5), include.mean = F)

Coefficients:
        ar1     ar2      ar3      ma1      ma2     ma3     ma4     ma5
      0.671  0.7420  -0.7722  -0.6932  -0.5326  0.7862  -0.136  0.2068
s.e.  0.038  0.0289   0.0355   0.0505   0.0502  0.0493   0.049  0.0392

sigma^2 estimated as 0.03825:  log likelihood = 154.88,  aic = -291.77
In [110]:
unem_rate_mod_1 = arima(unem_rate_ts, order = c(3,1,5))
unem_rate_mod_1
Call:
arima(x = unem_rate_ts, order = c(3, 1, 5))

Coefficients:
        ar1     ar2      ar3      ma1      ma2     ma3     ma4     ma5
      0.671  0.7420  -0.7722  -0.6932  -0.5326  0.7862  -0.136  0.2068
s.e.  0.038  0.0289   0.0355   0.0505   0.0502  0.0493   0.049  0.0392

sigma^2 estimated as 0.03825:  log likelihood = 154.88,  aic = -293.77
In [111]:
diff_unem_rate_mod = arima(diff_unem_rate_ts, order = c(3,0,5), include.mean = F)
diff_unem_rate_mod
Call:
arima(x = diff_unem_rate_ts, order = c(3, 0, 5), include.mean = F)

Coefficients:
         ar1     ar2      ar3      ma1      ma2     ma3     ma4     ma5
      0.6711  0.7420  -0.7722  -0.6932  -0.5326  0.7862  -0.136  0.2068
s.e.  0.0379  0.0289   0.0355   0.0505   0.0502  0.0493   0.049  0.0392

sigma^2 estimated as 0.03825:  log likelihood = 154.88,  aic = -293.77
In [112]:
plot_time_fig(unem_rate_mod$residuals, main = "Residuals")
plot_acf(unem_rate_mod$residuals, main = "ACF of Residuals")
No description has been provided for this image
No description has been provided for this image

Box-Ljung test

  • pp33 very briefly talks about how to choose lag in the Box.test
  • From the following testing result, we cannot reject the null-hypothesis. So the model specified is adequate.
In [113]:
length(unem_rate); log(length(unem_rate))
735
6.59987049921284
In [114]:
Box.test(unem_rate_mod$residuals, lag = 7, type = 'Ljung')
	Box-Ljung test

data:  unem_rate_mod$residuals
X-squared = 5.5481, df = 7, p-value = 0.5934
In [115]:
Box.test(unem_rate_mod$residuals, lag = 12, type = 'Ljung')
	Box-Ljung test

data:  unem_rate_mod$residuals
X-squared = 20.042, df = 12, p-value = 0.06629
  • Diagnose time-series ARIMA model
In [161]:
help(tsdiag)
In [162]:
par(bg = 'white')
tsdiag(unem_rate_mod, gof.lag=36)
No description has been provided for this image

Check business cycle

  • pp42
  • Compute the average length of business cycles
  • Doesn't matter if using the characteristic roots or the inverse of them.
  • Unit is "month". Average business cycle is about 14.2 months, which seems to be shoter than expected from the time plot above.
    • The business cycle is very sensitive to the order chosen in fitting ARIMA model. When I use ARIMA(2,0,5) or ARIMA(4,0,5), the business cycle doens't exist or too short.
In [69]:
find('arima')
  1. 'package:TSA'
  2. 'package:stats'
In [79]:
help("arima", package = "TSA")
In [80]:
help("arima", package = "stats")
In [116]:
sqrt(unem_rate_mod$sigma2)
0.195566624165521
In [117]:
unem_rate_mod$coef
ar1
0.671039253140843
ar2
0.741978426548034
ar3
-0.772184227253138
ma1
-0.693171209477596
ma2
-0.532634001385892
ma3
0.786216728216251
ma4
-0.135954862493427
ma5
0.206764447296786
In [118]:
ar_poly = c(1, -unem_rate_mod$coef[1:3]) # Characteristic equation for AR process
roots = polyroot(ar_poly)
roots
  1. 1.00437273858239+0.47656129169554i
  2. -1.04786282719477-0i
  3. 1.00437273858239-0.47656129169554i
In [119]:
for (i in 1:3) {
    print(paste("====", i))
    print(as.numeric(Mod(1-unem_rate_mod$coef[1]*roots[i]-unem_rate_mod$coef[2]*roots[i]^2-unem_rate_mod$coef[3]*roots[i]^3-unem_rate_mod$coef[4]*roots[i]^4)))
    print(as.numeric(Mod(roots[i])))
}
[1] "==== 1"
[1] 1.058741
[1] 1.111699
[1] "==== 2"
[1] 0.8357151
[1] 1.047863
[1] "==== 3"
[1] 1.058741
[1] 1.111699
  • Modulus of roots
In [120]:
roots; Mod(roots)
  1. 1.00437273858239+0.47656129169554i
  2. -1.04786282719477-0i
  3. 1.00437273858239-0.47656129169554i
  1. 1.11169926812516
  2. 1.04786282719477
  3. 1.11169926812516
In [121]:
1/roots; Mod(1/roots)
  1. 0.812681318944363-0.385606303531897i
  2. -9.54323384747883e-01+9e-16i
  3. 0.812681318944364+0.385606303531896i
  1. 0.899523844867207
  2. 0.954323384747883
  3. 0.899523844867207
  • Compute the average length of business cycles
  • Doesn't matter if using the characteristic roots or the inverse of them.
In [122]:
roots[1]; Re(roots[1]); Im(roots[1])
1.00437273858239+0.47656129169554i
1.00437273858239
0.476561291695542
In [125]:
k = 2*pi/acos(Re(roots[1])/Mod(roots[1]))
k
14.1823254232
In [126]:
k = 2*pi/acos(Re(1/roots[1])/Mod(1/roots[1]))
k
14.1823254232

Forecast

In [128]:
length(unem_rate_ts); unem_rate_ts
735
A Time Series: 62 × 12
JanFebMarAprMayJunJulAugSepOctNovDec
1948 3.4 3.8 4.0 3.9 3.5 3.6 3.6 3.9 3.8 3.7 3.8 4.0
1949 4.3 4.7 5.0 5.3 6.1 6.2 6.7 6.8 6.6 7.9 6.4 6.6
1950 6.5 6.4 6.3 5.8 5.5 5.4 5.0 4.5 4.4 4.2 4.2 4.3
1951 3.7 3.4 3.4 3.1 3.0 3.2 3.1 3.1 3.3 3.5 3.5 3.1
1952 3.2 3.1 2.9 2.9 3.0 3.0 3.2 3.4 3.1 3.0 2.8 2.7
1953 2.9 2.6 2.6 2.7 2.5 2.5 2.6 2.7 2.9 3.1 3.5 4.5
1954 4.9 5.2 5.7 5.9 5.9 5.6 5.8 6.0 6.1 5.7 5.3 5.0
1955 4.9 4.7 4.6 4.7 4.3 4.2 4.0 4.2 4.1 4.3 4.2 4.2
1956 4.0 3.9 4.2 4.0 4.3 4.3 4.4 4.1 3.9 3.9 4.3 4.2
1957 4.2 3.9 3.7 3.9 4.1 4.3 4.2 4.1 4.4 4.5 5.1 5.2
1958 5.8 6.4 6.7 7.4 7.4 7.3 7.5 7.4 7.1 6.7 6.2 6.2
1959 6.0 5.9 5.6 5.2 5.1 5.0 5.1 5.2 5.5 5.7 5.8 5.3
1960 5.2 4.8 5.4 5.2 5.1 5.4 5.5 5.6 5.5 6.1 6.1 6.6
1961 6.6 6.9 6.9 7.0 7.1 6.9 7.0 6.6 6.7 6.5 6.1 6.0
1962 5.8 5.5 5.6 5.6 5.5 5.5 5.4 5.7 5.6 5.4 5.7 5.5
1963 5.7 5.9 5.7 5.7 5.9 5.6 5.6 5.4 5.5 5.5 5.7 5.5
1964 5.6 5.4 5.4 5.3 5.1 5.2 4.9 5.0 5.1 5.1 4.8 5.0
1965 4.9 5.1 4.7 4.8 4.6 4.6 4.4 4.4 4.3 4.2 4.1 4.0
1966 4.0 3.8 3.8 3.8 3.9 3.8 3.8 3.8 3.7 3.7 3.6 3.8
1967 3.9 3.8 3.8 3.8 3.8 3.9 3.8 3.8 3.8 4.0 3.9 3.8
1968 3.7 3.8 3.7 3.5 3.5 3.7 3.7 3.5 3.4 3.4 3.4 3.4
1969 3.4 3.4 3.4 3.4 3.4 3.5 3.5 3.5 3.7 3.7 3.5 3.5
1970 3.9 4.2 4.4 4.6 4.8 4.9 5.0 5.1 5.4 5.5 5.9 6.1
1971 5.9 5.9 6.0 5.9 5.9 5.9 6.0 6.1 6.0 5.8 6.0 6.0
1972 5.8 5.7 5.8 5.7 5.7 5.7 5.6 5.6 5.5 5.6 5.3 5.2
1973 4.9 5.0 4.9 5.0 4.9 4.9 4.8 4.8 4.8 4.6 4.8 4.9
1974 5.1 5.2 5.1 5.1 5.1 5.4 5.5 5.5 5.9 6.0 6.6 7.2
1975 8.1 8.1 8.6 8.8 9.0 8.8 8.6 8.4 8.4 8.4 8.3 8.2
1976 7.9 7.7 7.6 7.7 7.4 7.6 7.8 7.8 7.6 7.7 7.8 7.8
1977 7.5 7.6 7.4 7.2 7.0 7.2 6.9 7.0 6.8 6.8 6.8 6.4
1978 6.4 6.3 6.3 6.1 6.0 5.9 6.2 5.9 6.0 5.8 5.9 6.0
1979 5.9 5.9 5.8 5.8 5.6 5.7 5.7 6.0 5.9 6.0 5.9 6.0
1980 6.3 6.3 6.3 6.9 7.5 7.6 7.8 7.7 7.5 7.5 7.5 7.2
1981 7.5 7.4 7.4 7.2 7.5 7.5 7.2 7.4 7.6 7.9 8.3 8.5
1982 8.6 8.9 9.0 9.3 9.4 9.6 9.8 9.810.110.410.810.8
198310.410.410.310.210.110.1 9.4 9.5 9.2 8.8 8.5 8.3
1984 8.0 7.8 7.8 7.7 7.4 7.2 7.5 7.5 7.3 7.4 7.2 7.3
1985 7.3 7.2 7.2 7.3 7.2 7.4 7.4 7.1 7.1 7.1 7.0 7.0
1986 6.7 7.2 7.2 7.1 7.2 7.2 7.0 6.9 7.0 7.0 6.9 6.6
1987 6.6 6.6 6.6 6.3 6.3 6.2 6.1 6.0 5.9 6.0 5.8 5.7
1988 5.7 5.7 5.7 5.4 5.6 5.4 5.4 5.6 5.4 5.4 5.3 5.3
1989 5.4 5.2 5.0 5.2 5.2 5.3 5.2 5.2 5.3 5.3 5.4 5.4
1990 5.4 5.3 5.2 5.4 5.4 5.2 5.5 5.7 5.9 5.9 6.2 6.3
1991 6.4 6.6 6.8 6.7 6.9 6.9 6.8 6.9 6.9 7.0 7.0 7.3
1992 7.3 7.4 7.4 7.4 7.6 7.8 7.7 7.6 7.6 7.3 7.4 7.4
1993 7.3 7.1 7.0 7.1 7.1 7.0 6.9 6.8 6.7 6.8 6.6 6.5
1994 6.6 6.6 6.5 6.4 6.1 6.1 6.1 6.0 5.9 5.8 5.6 5.5
1995 5.6 5.4 5.4 5.8 5.6 5.6 5.7 5.7 5.6 5.5 5.6 5.6
1996 5.6 5.5 5.5 5.6 5.6 5.3 5.5 5.1 5.2 5.2 5.4 5.4
1997 5.3 5.2 5.2 5.1 4.9 5.0 4.9 4.8 4.9 4.7 4.6 4.7
1998 4.6 4.6 4.7 4.3 4.4 4.5 4.5 4.5 4.6 4.5 4.4 4.4
1999 4.3 4.4 4.2 4.3 4.2 4.3 4.3 4.2 4.2 4.1 4.1 4.0
2000 4.0 4.1 4.0 3.8 4.0 4.0 4.0 4.1 3.9 3.9 3.9 3.9
2001 4.2 4.2 4.3 4.4 4.3 4.5 4.6 4.9 5.0 5.3 5.5 5.7
2002 5.7 5.7 5.7 5.9 5.8 5.8 5.8 5.7 5.7 5.7 5.9 6.0
2003 5.8 5.9 5.9 6.0 6.1 6.3 6.2 6.1 6.1 6.0 5.8 5.7
2004 5.7 5.6 5.8 5.6 5.6 5.6 5.5 5.4 5.4 5.5 5.4 5.4
2005 5.2 5.4 5.2 5.2 5.1 5.1 5.0 4.9 5.0 5.0 5.0 4.8
2006 4.7 4.8 4.7 4.7 4.7 4.6 4.7 4.7 4.5 4.4 4.5 4.4
2007 4.6 4.5 4.4 4.5 4.4 4.6 4.6 4.6 4.7 4.7 4.7 5.0
2008 5.0 4.8 5.1 5.0 5.4 5.5 5.8 6.1 6.2 6.6 6.9 7.4
2009 7.7 8.2 8.6
In [140]:
npts = 8
eotr = length(unem_rate_ts)
h = 4
freq = 12
order = c(3,1,5)
fixed = NULL
seasonal = list(order = c(0L, 0L, 0L), period = NA)
unem_rate_fc_res = plot_arima_forecast_fig(
    da_ts=unem_rate_ts, eotr=eotr, h=h, npts=npts, frequency=freq, 
    order=order, seasonal=seasonal, fixed=fixed, method='ML', 
    include.mean=F, transform.pars=TRUE,
    main="Forecasts from ARIMA(3,1,5) for unemp_rate", 
    xlab="Year", ylab="Unemp-Rate", ylim=c(5, 11)
)
[1] 734
2008.5 ; 2009.583
No description has been provided for this image

Alternative solutions (only use AR model)

In [147]:
par(bg = 'white')
# decay slowly
acf(unem_rate)
par(mfrow = c(2, 1))
acf(diff(unem_rate))
pacf(diff(unem_rate))
acf(diff(unem_rate, 12))
pacf(diff(unem_rate, 12))
cat("order =", ar(unem_rate, method = 'mle')$order, "\n")
m1 = arima(unem_rate_ts, order = c(11, 0, 0))
m1
# Follow the pp51 to modify and refine the model based on previous fit
m1 = arima(unem_rate_ts, order = c(11, 0, 0), fixed = c(NA, NA, 0, 0, 0, NA, 0, 0, 0, NA, NA, NA))
m1
# FIXME
m2 = arima(unem_rate_ts, order = c(2, 1, 1), seasonal = list(order = c(1, 0, 1), period = 12))
m2
tsdiag(m1, gof = 36)
tsdiag(m2, gof = 36)
predict(m1, 4)
predict(m2, 4)
No description has been provided for this image
No description has been provided for this image
order = 11 
Call:
arima(x = unem_rate_ts, order = c(11, 0, 0))

Coefficients:
         ar1     ar2      ar3      ar4     ar5      ar6      ar7     ar8
      0.9886  0.2375  -0.0741  -0.0630  0.0301  -0.1283  -0.0426  0.0539
s.e.  0.0367  0.0516   0.0525   0.0525  0.0526   0.0524   0.0526  0.0527
          ar9     ar10    ar11  intercept
      -0.0146  -0.1293  0.1259     5.6554
s.e.   0.0526   0.0518  0.0371     0.4422

sigma^2 estimated as 0.03867:  log likelihood = 150.03,  aic = -276.07
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“some AR parameters were fixed: setting transform.pars = FALSE”
Call:
arima(x = unem_rate_ts, order = c(11, 0, 0), fixed = c(NA, NA, 0, 0, 0, NA, 
    0, 0, 0, NA, NA, NA))

Coefficients:
         ar1     ar2  ar3  ar4  ar5      ar6  ar7  ar8  ar9     ar10    ar11
      0.9805  0.1695    0    0    0  -0.1728    0    0    0  -0.1216  0.1282
s.e.  0.0361  0.0432    0    0    0   0.0270    0    0    0   0.0433  0.0364
      intercept
         5.6605
s.e.     0.4337

sigma^2 estimated as 0.03904:  log likelihood = 146.55,  aic = -281.11
Call:
arima(x = unem_rate_ts, order = c(2, 1, 1), seasonal = list(order = c(1, 0, 
    1), period = 12))

Coefficients:
         ar1     ar2      ma1    sar1     sma1
      0.5801  0.2444  -0.5813  0.5609  -0.8185
s.e.  0.0637  0.0398   0.0588  0.0734   0.0538

sigma^2 estimated as 0.03725:  log likelihood = 164.28,  aic = -318.56
No description has been provided for this image
No description has been provided for this image
$pred
A Time Series: 1 × 4
AprMayJunJul
20098.7967658.9819969.1125559.246428
$se
A Time Series: 1 × 4
AprMayJunJul
20090.19758560.27671340.35565800.4358310
$pred
A Time Series: 1 × 4
AprMayJunJul
20098.8418449.0171549.1269179.231977
$se
A Time Series: 1 × 4
AprMayJunJul
20090.19300980.27280390.36323950.4508560
No description has been provided for this image
In [149]:
ar_poly_1 = c(1, -m1$coef[1:11]) # Characteristic equation for AR process
roots_1 = polyroot(ar_poly_1)
roots_1
  1. 0.72130962198895+1.08701346704084i
  2. -1.14274870161967+0.39895294351557i
  3. -0.772376200376323-0.973913718979966i
  4. 1.135449573239+0.20175780984644i
  5. 0.01445275970928+1.21215403312275i
  6. -0.772376200376323+0.973913718979966i
  7. 0.01445275970928-1.21215403312275i
  8. 1.03577871789383+0i
  9. 1.135449573239-0.20175780984644i
  10. -1.14274870161967-0.39895294351557i
  11. 0.72130962198895-1.08701346704084i
In [155]:
Im(roots_1[8])
3.31804353375726e-15
In [156]:
for (i in 1:length(roots_1)) {
    if (abs(Im(roots_1[i]))>1e-8) {
        print(i)
        print(roots_1[i])
        print(2*pi/acos(Re(roots_1[i])/Mod(roots_1[i])))
    }
}
[1] 1
[1] 0.72131+1.087013i
[1] 6.379253
[1] 2
[1] -1.142749+0.398953i
[1] 2.239432
[1] 3
[1] -0.7723762-0.9739137i
[1] 2.803374
[1] 4
[1] 1.13545+0.201758i
[1] 35.72949
[1] 5
[1] 0.014453+1.212154i
[1] 4.030593
[1] 6
[1] -0.7723762+0.9739137i
[1] 2.803374
[1] 7
[1] 0.014453-1.212154i
[1] 4.030593
[1] 9
[1] 1.13545-0.201758i
[1] 35.72949
[1] 10
[1] -1.142749-0.398953i
[1] 2.239432
[1] 11
[1] 0.72131-1.087013i
[1] 6.379253
In [151]:
ar_poly_2 = c(1, -m2$coef[1:2]) # Characteristic equation for AR process
roots_2 = polyroot(ar_poly_2)
roots_2
  1. 1.158427203051+0i
  2. -3.53236324765541-0i

2-4 (CRSP Decile Index)

  • Data are monthly simple returns.
  • Interesting observations.
    • The log(dec2_rtn) shows medium momentum correlation in pacf and acf at lag-1 and lag-12. The log(dec10_rtn) shows almost no serial correlation. See plot_pacf_acf results.
In [546]:
da = read.table("../AFTS_data/Ch02/m-deciles08.txt", header = TRUE)
dim(da); da[1:5,]
  1. 468
  2. 5
A data.frame: 5 × 5
dateCAP1RETCAP2RETCAP9RETCAP10RET
<int><dbl><dbl><dbl><dbl>
119700130 0.054383-0.004338-0.073082-0.076874
219700227 0.020264 0.020155 0.064185 0.059512
319700331-0.031790-0.028090-0.004034-0.001327
419700430-0.184775-0.193004-0.115825-0.091112
519700529-0.088189-0.085342-0.085565-0.053193
In [547]:
dec2 = da$CAP2RET
dec2_ts = ts(da$CAP2RET, frequency = 12, start = c(1970, 1))
dec10 = da$CAP10RET
dec10_ts = ts(da$CAP10RET, frequency = 12, start = c(1970, 1))
In [5]:
plot_time_fig(dec2_ts, main = "CRSP Decile Index", xlab = "Year")
lines(dec10_ts, type = , col = 'red', lty = 2)
legend(
    "topright", 
    legend = c("Decile 2", "Decile 10"), 
    col = c("black", "red"), 
    lty = c(1, 2),
    pch = c(8, NA)
)
No description has been provided for this image
In [548]:
plot_pacf_acf(dec2, freq = 12, lag.max = 36)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [551]:
plot_pacf_acf(log(1+dec2), freq = 12, lag.max = 36)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [549]:
plot_pacf_acf(dec10, freq = 12, lag.max = 36)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [552]:
plot_pacf_acf(log(1+dec10), freq = 12, lag.max = 36)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image

(a) Test autocorrelations

  • Reject the null-hypothesis (i.e., the first 12 lags have no serial correlation) for Decile 2. So for Decile 2, the first 12 lags have serial correlation.
  • Cannot reject the null-hypothesis for Decile 10. So for Decile 10, the first 12 lags have no significant serial correlation.
In [6]:
par(mfrow = c(2, 1), bg = 'white')
acf(da$CAP2RET)
pacf(da$CAP2RET)
Box.test(da$CAP2RET, lag = 12, type = 'Ljung')
	Box-Ljung test

data:  da$CAP2RET
X-squared = 55.736, df = 12, p-value = 1.335e-07
No description has been provided for this image
In [7]:
par(mfrow = c(2, 1), bg = 'white')
acf(da$CAP10RET)
pacf(da$CAP10RET)
Box.test(da$CAP10RET, lag = 12, type = 'Ljung')
	Box-Ljung test

data:  da$CAP10RET
X-squared = 10.687, df = 12, p-value = 0.5559
No description has been provided for this image

(b) Fit a model and perform model checking

In [8]:
dec2_eacf_res <- perform_and_print_eacf(da$CAP2RET, ar.max = 25, ma.max = 12)
A data.frame: 26 × 13
0123456789101112
<I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>>
1 0.20369-0.03114-0.0617-0.03854-0.04448-0.01246-1.80e-02-0.08682-0.05043 0.044819 0.03371 0.23095 0.02117
2 0.32892-0.10631-0.0375 0.01921-0.03447 0.02160 3.98e-03-0.06861-0.05926 0.066053-0.00460 0.23682 0.06396
3-0.42524 0.01475-0.1102-0.01244-0.01521 0.00803 7.46e-03-0.04457-0.04307 0.020265 0.01009 0.23522-0.05571
4-0.40167-0.05250-0.0518 0.06362-0.00120 0.01725 2.15e-03-0.02434-0.00964 0.024374-0.02813 0.23364-0.13191
5-0.40022-0.43254-0.2838-0.00515-0.03041 0.01686 2.73e-03-0.00353-0.02382-0.007906-0.00472 0.23347-0.06624
6-0.06468-0.43194-0.1068 0.12936-0.06677-0.04765-4.82e-03 0.00172-0.02487 0.000663-0.00560 0.23064-0.03428
7-0.10539-0.02918-0.3781-0.13146-0.13792 0.01120-1.39e-02-0.00394-0.02474-0.006610-0.00960 0.21633-0.12140
8-0.25901 0.04140-0.3007 0.12273-0.00148-0.11108-4.85e-05 0.03159 0.00131-0.003169-0.02814 0.17372-0.12466
9-0.24123 0.39312-0.2237 0.09617-0.00180-0.10194 6.02e-05 0.02881 0.01067-0.003566-0.03400 0.15280-0.10936
10 0.39313 0.35825 0.0508 0.25484 0.06186-0.05278 1.18e-01 0.03313 0.00458-0.005226-0.04365 0.08590-0.12888
11 0.02324-0.16438 0.0591 0.29471 0.14351-0.01095 1.10e-01 0.03473-0.04732 0.039074-0.02754 0.01843-0.12941
12 0.00277-0.19923 0.1018 0.30826 0.02356-0.05258 1.38e-01 0.12985 0.08072 0.153820-0.42198-0.00799-0.06233
13 0.29698 0.23296 0.0675 0.05170-0.05332 0.08408 2.25e-01 0.12233 0.20471 0.187092 0.07290 0.02734-0.08006
14-0.41434 0.15559-0.1088 0.03756 0.00361 0.02920 2.07e-01-0.10366 0.03855 0.207229 0.03509-0.18728 0.01939
15 0.27098-0.00392 0.0464 0.00807-0.01820 0.03644 1.96e-01-0.10690 0.03093 0.170090 0.04863-0.03330 0.01009
16 0.32328-0.00167 0.0780-0.00315-0.08522 0.00227 1.16e-01 0.06426 0.10021 0.061636 0.05065-0.04054-0.02312
17 0.19872-0.24388 0.0253-0.03706-0.08837 0.00731 5.98e-02-0.03659 0.08578-0.027778 0.11103-0.01494-0.01742
18 0.45276 0.05983 0.0949-0.08135-0.26125 0.03599 1.02e-01 0.01364 0.05305 0.031292 0.11314-0.07872-0.00302
19-0.39023 0.35687-0.2743 0.26657-0.15358 0.05311 9.46e-02-0.10945-0.01772 0.034096 0.13725 0.02100 0.00207
20 0.13505-0.37359-0.4908 0.21703 0.27756 0.18885 2.24e-03-0.06054-0.04551 0.019328 0.07323-0.08491 0.01630
21 0.21470-0.18877-0.4752 0.14720 0.39244 0.15284-5.28e-03-0.12773 0.03177-0.008420 0.07184-0.00911 0.00216
22-0.46086 0.30565-0.1895 0.15917 0.36093-0.05730 1.77e-01-0.08461 0.02247-0.002969 0.04152-0.04145-0.02601
23 0.37876 0.48961 0.3502 0.07946 0.13617-0.10137 2.15e-01-0.05946 0.00999 0.043435 0.06366-0.01055-0.01546
24-0.37059 0.39806-0.1081 0.19561 0.26711 0.10149 2.48e-01 0.00868 0.10901 0.107988 0.06787-0.04747 0.07475
25-0.22999-0.42493 0.0483 0.38694-0.16429-0.24948 2.03e-01 0.28052 0.08915-0.240086-0.00779 0.21276-0.04742
26-0.16376-0.37835 0.0813 0.30938-0.14831-0.34927 1.43e-01 0.26769 0.06260-0.239522-0.01508 0.10886-0.08528
A matrix: 26 × 13 of type chr
0123456789101112
0xooooooooooxo
1xxoooooooooxo
2xoxooooooooxo
3xooooooooooxx
4xxxooooooooxo
5oxxxoooooooxo
6xoxxxooooooxx
7xoxxoxoooooxx
8xxxxoxoooooxx
9xxoxooxooooox
10oxoxxoxooooox
11oxxxooxxoxxoo
12xxooooxxxxooo
13xxxoooxxoxoxo
14xoooooxxoxooo
15xoooooxoxoooo
16xxooooooooxoo
17xoxoxoxoooxoo
18xxxxxooxooxoo
19xxxxxxooooooo
20xxxxxxoxooooo
21xxxxxoxoooooo
22xxxoxxxoooooo
23xxxxxxxoxxooo
24xxoxxxxxoxoxo
25xxoxxxxxoxoxo
0.0924500327042048
AR/MA
   0 1 2 3 4 5 6 7 8 9 10 11 12
0  x o o o o o o o o o o  x  o 
1  x x o o o o o o o o o  x  o 
2  x o x o o o o o o o o  x  o 
3  x o o o o o o o o o o  x  x 
4  x x x o o o o o o o o  x  o 
5  o x x x o o o o o o o  x  o 
6  x o x x x o o o o o o  x  x 
7  x o x x o x o o o o o  x  x 
8  x x x x o x o o o o o  x  x 
9  x x o x o o x o o o o  o  x 
10 o x o x x o x o o o o  o  x 
11 o x x x o o x x o x x  o  o 
12 x x o o o o x x x x o  o  o 
13 x x x o o o x x o x o  x  o 
14 x o o o o o x x o x o  o  o 
15 x o o o o o x o x o o  o  o 
16 x x o o o o o o o o x  o  o 
17 x o x o x o x o o o x  o  o 
18 x x x x x o o x o o x  o  o 
19 x x x x x x o o o o o  o  o 
20 x x x x x x o x o o o  o  o 
21 x x x x x o x o o o o  o  o 
22 x x x o x x x o o o o  o  o 
23 x x x x x x x o x x o  o  o 
24 x x o x x x x x o x o  x  o 
25 x x o x x x x x o x o  x  o 
  • Fit ARIMA(1,0,1) model, b/c only this model has all coefs significant.
In [10]:
dec2_mod_1 <- arima(da$CAP2RET, order = c(1, 0, 1), include.mean = T)
dec2_mod_1; Box.test(dec2_mod_1$residuals, lag = 12, type = 'Ljung');
par(bg = 'white')
tsdiag(dec2_mod_1, gof.lag = 36)
Call:
arima(x = da$CAP2RET, order = c(1, 0, 1), include.mean = T)

Coefficients:
          ar1     ma1  intercept
      -0.0260  0.2433     0.0105
s.e.   0.1773  0.1702     0.0036

sigma^2 estimated as 0.004077:  log likelihood = 623.48,  aic = -1240.96
	Box-Ljung test

data:  dec2_mod_1$residuals
X-squared = 36.405, df = 12, p-value = 0.0002788
No description has been provided for this image
In [11]:
# Follow the pp51 to modify and refine the model based on previous fit
dec2_mod_2 <- arima(da$CAP2RET, order = c(0, 0, 1), include.mean = T)
dec2_mod_2; Box.test(dec2_mod_2$residuals, lag = 10, type = 'Ljung');
par(bg = 'white')
tsdiag(dec2_mod_2, gof.lag = 36)
Call:
arima(x = da$CAP2RET, order = c(0, 0, 1), include.mean = T)

Coefficients:
         ma1  intercept
      0.2191     0.0105
s.e.  0.0449     0.0036

sigma^2 estimated as 0.004077:  log likelihood = 623.47,  aic = -1242.93
	Box-Ljung test

data:  dec2_mod_2$residuals
X-squared = 8.1094, df = 10, p-value = 0.6182
No description has been provided for this image
In [12]:
dec2_mod_3 <- arima(da$CAP2RET, order = c(4, 0, 3), include.mean = T)
dec2_mod_3; Box.test(dec2_mod_3$residuals, lag = 12, type = 'Ljung');
par(bg = 'white')
tsdiag(dec2_mod_3, gof.lag = 36)
Call:
arima(x = da$CAP2RET, order = c(4, 0, 3), include.mean = T)

Coefficients:
         ar1     ar2      ar3     ar4      ma1      ma2     ma3  intercept
      0.7561  0.7891  -0.7599  0.2034  -0.5337  -0.9888  0.5226     0.0117
s.e.  0.6190  0.1121   0.6092  0.1197   0.6306   0.0121  0.6263     0.0013

sigma^2 estimated as 0.003948:  log likelihood = 629.86,  aic = -1243.71
	Box-Ljung test

data:  dec2_mod_3$residuals
X-squared = 30.035, df = 12, p-value = 0.002759
No description has been provided for this image
In [13]:
dec2_mod_3 <- arima(
    da$CAP2RET, 
    order = c(4, 0, 3), 
    # Follow the pp51 to modify and refine the model based on previous fit
    fixed = c(0, NA, 0, NA, 0, NA, 0, NA),
    include.mean = T
)
dec2_mod_3; Box.test(dec2_mod_3$residuals, lag = 12, type = 'Ljung');
par(bg = 'white')
tsdiag(dec2_mod_3, gof.lag = 36)
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“some AR parameters were fixed: setting transform.pars = FALSE”
Call:
arima(x = da$CAP2RET, order = c(4, 0, 3), include.mean = T, fixed = c(0, NA, 
    0, NA, 0, NA, 0, NA))

Coefficients:
      ar1     ar2  ar3      ar4  ma1      ma2  ma3  intercept
        0  0.3407    0  -0.0362    0  -0.3752    0     0.0106
s.e.    0  0.3787    0   0.0507    0   0.3767    0     0.0027

sigma^2 estimated as 0.004255:  log likelihood = 613.47,  aic = -1218.94
	Box-Ljung test

data:  dec2_mod_3$residuals
X-squared = 51.445, df = 12, p-value = 7.773e-07
No description has been provided for this image

(c) Forecast using ARMA model

In [22]:
npts = 12
eotr = length(dec2_ts)-npts
h = npts
freq = 12
order = c(0,0,1)
fixed = NULL
seasonal = NULL
dec2_fc_res = plot_arima_forecast_fig(
    da_ts=dec2_ts, eotr=eotr, h=h, npts=npts, frequency=freq, 
    order=order, seasonal=seasonal, fixed=fixed, method='ML', 
    include.mean=T, transform.pars=TRUE,
    main="Forecasts from ARIMA(0,0,1) for Decile 2 index", 
    xlab="Year", ylab="Unemp-Rate"# , ylim=c(5, 11)
)
dec2_fc_tb = comb_forecast_res(dec2_fc_res, dec2_ts, eotr, freq)
dec2_fc_tb
[1] 456
2006.917 ; 2009
Forecast method: ARIMA(0,0,1) with non-zero mean

Model Information:

Call:
arima(x = tr_da_ts, order = order, seasonal = seasonal, include.mean = include.mean, 
    fixed = fixed, method = method)

Coefficients:
         ma1  intercept
      0.2043     0.0119
s.e.  0.0464     0.0036

sigma^2 estimated as 0.004074:  log likelihood = 607.66,  aic = -1211.33

Error measures:
                       ME       RMSE       MAE      MPE     MAPE      MASE
Training set 1.545115e-06 0.06382686 0.0433542 100.5658 186.3821 0.7337148
                     ACF1
Training set -0.004704323

Forecasts:
         Point Forecast       Lo 80      Hi 80      Lo 95     Hi 95
Jan 2008    0.009250982 -0.07254643 0.09104840 -0.1158474 0.1343493
Feb 2008    0.011867122 -0.07161973 0.09535397 -0.1158150 0.1395492
Mar 2008    0.011867122 -0.07161973 0.09535397 -0.1158150 0.1395492
Apr 2008    0.011867122 -0.07161973 0.09535397 -0.1158150 0.1395492
May 2008    0.011867122 -0.07161973 0.09535397 -0.1158150 0.1395492
Jun 2008    0.011867122 -0.07161973 0.09535397 -0.1158150 0.1395492
Jul 2008    0.011867122 -0.07161973 0.09535397 -0.1158150 0.1395492
Aug 2008    0.011867122 -0.07161973 0.09535397 -0.1158150 0.1395492
Sep 2008    0.011867122 -0.07161973 0.09535397 -0.1158150 0.1395492
Oct 2008    0.011867122 -0.07161973 0.09535397 -0.1158150 0.1395492
Nov 2008    0.011867122 -0.07161973 0.09535397 -0.1158150 0.1395492
Dec 2008    0.011867122 -0.07161973 0.09535397 -0.1158150 0.1395492
A Time Series: 1 × 12
JanFebMarAprMayJunJulAugSepOctNovDec
20080.0092509820.0118671220.0118671220.0118671220.0118671220.0118671220.0118671220.0118671220.0118671220.0118671220.0118671220.011867122
A Time Series: 1 × 12
JanFebMarAprMayJunJulAugSepOctNovDec
20080.063826860.065145130.065145130.065145130.065145130.065145130.065145130.065145130.065145130.065145130.065145130.06514513
A Time Series: 1 × 12
JanFebMarAprMayJunJulAugSepOctNovDec
2008-0.008644-0.017025-0.024443 0.015660 0.034079-0.037152-0.048765-0.012646-0.132327-0.129844-0.114484-0.022210
A Time Series: 12 × 3
ForecastStd. ErrorActual
Jan 20080.0092509820.06382686-0.008644
Feb 20080.0118671220.06514513-0.017025
Mar 20080.0118671220.06514513-0.024443
Apr 20080.0118671220.06514513 0.015660
May 20080.0118671220.06514513 0.034079
Jun 20080.0118671220.06514513-0.037152
Jul 20080.0118671220.06514513-0.048765
Aug 20080.0118671220.06514513-0.012646
Sep 20080.0118671220.06514513-0.132327
Oct 20080.0118671220.06514513-0.129844
Nov 20080.0118671220.06514513-0.114484
Dec 20080.0118671220.06514513-0.022210
No description has been provided for this image
In [17]:
class(dec2_fc_res); attributes(dec2_fc_res); dec2_fc_res$model
'forecast'
$names
  1. 'method'
  2. 'model'
  3. 'level'
  4. 'mean'
  5. 'lower'
  6. 'upper'
  7. 'x'
  8. 'series'
  9. 'fitted'
  10. 'residuals'
$class
'forecast'
Call:
arima(x = tr_da_ts, order = order, seasonal = seasonal, include.mean = include.mean, 
    fixed = fixed, method = method)

Coefficients:
         ma1  intercept
      0.2043     0.0119
s.e.  0.0464     0.0036

sigma^2 estimated as 0.004074:  log likelihood = 607.66,  aic = -1211.33
In [20]:
help("predict")
In [21]:
predict(dec2_fc_res$model, 12)
$pred
A Time Series: 1 × 12
JanFebMarAprMayJunJulAugSepOctNovDec
20080.0092509820.0118671220.0118671220.0118671220.0118671220.0118671220.0118671220.0118671220.0118671220.0118671220.0118671220.011867122
$se
A Time Series: 1 × 12
JanFebMarAprMayJunJulAugSepOctNovDec
20080.063826860.065145130.065145130.065145130.065145130.065145130.065145130.065145130.065145130.065145130.065145130.06514513
In [23]:
dec2_fc_tb
A Time Series: 12 × 3
ForecastStd. ErrorActual
Jan 20080.0092509820.06382686-0.008644
Feb 20080.0118671220.06514513-0.017025
Mar 20080.0118671220.06514513-0.024443
Apr 20080.0118671220.06514513 0.015660
May 20080.0118671220.06514513 0.034079
Jun 20080.0118671220.06514513-0.037152
Jul 20080.0118671220.06514513-0.048765
Aug 20080.0118671220.06514513-0.012646
Sep 20080.0118671220.06514513-0.132327
Oct 20080.0118671220.06514513-0.129844
Nov 20080.0118671220.06514513-0.114484
Dec 20080.0118671220.06514513-0.022210

2-5 (IBM)

  • Data are daily simple returns.
  • There is some long-range dependency in daily IBM simple returns.
In [561]:
da = read.table("../AFTS_sol/data/d-ibm3dx7008.txt", header = TRUE)
ibm = da$rtn
da[1:5,]
A data.frame: 5 × 5
Datertnvwretdewretdsprtrn
<int><dbl><dbl><dbl><dbl>
1197001020.000686 0.012137 0.033450 0.010211
2197001050.009596 0.006375 0.018947 0.004946
3197001060.000679-0.007233-0.005776-0.006848
4197001070.000678-0.001272 0.003559-0.002047
5197001080.002034 0.000564 0.002890 0.000540
In [25]:
plot_acf(abs(ibm), lag.max = 100)
No description has been provided for this image

2-6 (Monthly Power Consumption)

  • This is a multiplicative seasonal model.
  • The seasonal prediction is very good.
  • The data set in this problem bears a lot similarity with the JnJ quarterly earning data set in Sec. 2.8. After taking one regular difference, the seasonality becomes very obvious. This means that after taking one regular difference, we need to model the AR or MA pattern of the resulting time series.
  • The logic of determining the orders in arima model from the various pacf and acf plots can be as following.
    • The plots for pow show strong serial correlation. So we need to take diff.
    • The plots for diff(pow) show strong seasonality in the acf. So we need to take the seasonal diff.
    • The plots for diff(pow, 12) show some autocorrelation in the acf. So we need to take both diff and seasonal diff.
    • The plots for diff(diff(pow), 12) show some autocorrelation in the acf. So we need to apply MA model on diff(diff(pow), 12). Thus this is a multiplicative seasonal model.
  • Interesting observations.
    • The diff(log(pow)) shows first strong momentum, second strong mean-reverting, and serial correlation in pacf and acf (b/c of seasonality). So the power-consumption time series doesn't satisfy "efficient market theory". So finanical instruments that are created around this time series have arbitrage opportunities. See plot_pacf_acf results.
In [502]:
da = read.table("../AFTS_sol/data/power6.txt", header = FALSE)
dim(da); da[1:5,]
  1. 264
  2. 1
  1. 9.2691
  2. 9.2465
  3. 9.2639
  4. 9.2784
  5. 9.3147
In [507]:
par(bg = 'white')
pow = da[, 1]
pow_ts = ts(pow, frequency = 12, start = c(2000, 1))
# From the time plot, should immediately think that we can apply multiplicative seasonal model
plot(pow_ts, type = 'o', xlab = 'Year', ylab = 'Power', main = "Power Consumption")

plot(diff(pow_ts), type = 'o', xlab = 'Year', ylab = 'diff(Power)', main = "Power Consumption")

plot(diff(pow_ts, 12), type = 'o', xlab = 'Year', ylab = 'diff(Power, 12)', main = "Power Consumption")

plot(diff(diff(pow_ts), 12), type = 'o', xlab = 'Year', ylab = 'diff(diff(Power), 12)', main = "Power Consumption")
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [542]:
plot_pacf_acf(pow, freq = 12, lag.max = 48)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [508]:
par(bg = 'white')
lg_pow = log(da[, 1])
lg_pow_ts = ts(lg_pow, frequency = 12, start = c(2000, 1))
# From the time plot, should immediately think that we can apply multiplicative seasonal model
plot(lg_pow_ts, type = 'o', xlab = 'Year', ylab = 'lg_power', main = "lg_power Consumption")

plot(diff(lg_pow_ts), type = 'o', xlab = 'Year', ylab = 'diff(lg_power)', main = "lg_power Consumption")

plot(diff(lg_pow_ts, 12), type = 'o', xlab = 'Year', ylab = 'diff(lg_power, 12)', main = "lg_power Consumption")

plot(diff(diff(lg_pow_ts), 12), type = 'o', xlab = 'Year', ylab = 'diff(diff(lg_power), 12)', main = "lg_power Consumption")
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [541]:
plot_pacf_acf(lg_pow, freq = 12, lag.max = 48)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [ ]:

In [103]:
# The eacf is not helpful in determining the order of ARIMA model
pow_eacf_res <- perform_and_print_eacf(pow, ar.max = 25, ma.max = 12)
A data.frame: 26 × 13
0123456789101112
<I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>>
1 0.96571 0.9080 0.8377 0.7655 0.71498 0.69168 0.69440 0.72362 0.773267 0.8211 0.8573 0.86632 0.8387
2 0.48462 0.2868 0.0751-0.3656-0.49156-0.47986-0.42300-0.27176 0.055811 0.2656 0.5053 0.75737 0.5252
3-0.12526 0.2006 0.0500-0.2874-0.08769-0.01527-0.13087-0.29585 0.084182 0.0655 0.0642 0.48727 0.2065
4 0.40583 0.0580 0.1825-0.2774-0.05706-0.00408-0.00607-0.22609 0.164512-0.0636-0.0280 0.40705-0.0743
5-0.23966 0.1946 0.5000-0.2768-0.04448 0.05621-0.00483-0.21901 0.179330-0.0263-0.0688 0.41287-0.0364
6-0.40472-0.1180-0.3035-0.1740 0.09863-0.05222 0.10280-0.23891-0.089101-0.0882 0.0255 0.25582 0.0064
7-0.49066 0.0998-0.0943-0.1726 0.32596 0.16719 0.15146-0.22323 0.052400-0.1301 0.0103 0.24534 0.0243
8-0.43350-0.1371-0.0570-0.2257 0.39052-0.02031 0.10420-0.21595-0.124124-0.0587-0.0169 0.15903-0.0706
9-0.46920 0.0606-0.1957-0.2735 0.39116-0.01460 0.29364-0.19086 0.088586-0.0716-0.0433 0.22520-0.0638
10-0.40164-0.1888 0.1925 0.4526-0.14736-0.19761 0.16804 0.23199-0.063995-0.1397 0.0650-0.02067 0.0570
11-0.50251-0.3665 0.0424 0.5069-0.18772-0.32381 0.12976 0.28136-0.066008-0.2720 0.0595-0.00967-0.0380
12-0.01093 0.1967-0.1065 0.3678 0.14461 0.12197 0.07902 0.00539 0.137283-0.0978 0.0701 0.06164-0.0754
13-0.17885 0.2982 0.2797 0.3059-0.11268 0.03445 0.05553-0.02337 0.112943-0.1702 0.1513 0.03375-0.0246
14-0.49983 0.2406-0.2440 0.1704-0.10629 0.04095 0.07715-0.02402 0.032608-0.0461 0.0347-0.22437 0.1587
15-0.22348-0.3658-0.2354-0.0464 0.04177 0.08093 0.10552 0.09814-0.003118-0.0311-0.0309-0.38593 0.0145
16-0.40200-0.2337-0.1751-0.1385 0.02525-0.01050 0.11290 0.22483-0.066749-0.0521-0.0354-0.39753 0.1508
17-0.47804 0.1087-0.0335 0.0536 0.01371-0.00288 0.12952 0.20014-0.026307 0.0209-0.0252-0.32491 0.0708
18-0.43757-0.0678 0.0226 0.0106 0.00705-0.02970 0.16429 0.02475-0.031768 0.0287-0.0378-0.37439 0.1403
19-0.44875-0.0350 0.0283 0.0140 0.04629-0.16741-0.00992 0.03302 0.051821-0.0152-0.2024-0.40911-0.1789
20-0.10704 0.0546-0.0268 0.0597 0.26460-0.17678 0.01269 0.01676 0.053342-0.0303-0.1867-0.37761 0.1276
21 0.00603 0.1789 0.1906 0.3505 0.09617-0.05645 0.08630-0.05548 0.005178-0.0115-0.1248-0.33770 0.0559
22-0.34425-0.4013 0.1859 0.4218 0.14376-0.04038 0.21477-0.06536 0.000518-0.0341-0.0703-0.34027-0.0386
23-0.40302-0.4847 0.0806 0.4274 0.04296-0.15622 0.10458-0.07416-0.106884-0.0618-0.0455-0.34531-0.0273
24-0.24496 0.3079-0.1119 0.4502 0.14402 0.19132 0.11027-0.07358-0.103901-0.0729 0.0061-0.34701-0.1540
25 0.09975 0.4002 0.1389 0.4044-0.13304 0.10269 0.00357-0.08368 0.014541-0.2153-0.1404-0.35439 0.1572
26-0.44069-0.0591-0.1255 0.3419-0.20926 0.10856 0.00282-0.03992 0.104176-0.1713 0.1495-0.32197-0.1242
A matrix: 26 × 13 of type chr
0123456789101112
0xxxxxxxxxxxxx
1xxoxxxxxoxxxx
2xxoxooxxoooxx
3xoxxoooxxooxo
4xxxxoooxxooxo
5xoxxoooxoooxo
6xooxxxxxoxoxo
7xxoxxooxoooxo
8xoxxxoxxoooxo
9xxxxxxxxoxooo
10xxoxxxxxoxooo
11oxoxxoooxoooo
12xxxxoooooxxoo
13xxxxoooooooxx
14xxxooooooooxo
15xxxxoooxoooxx
16xoooooxxoooxo
17xoooooxooooxx
18xooooxooooxxx
19ooooxxooooxxo
20oxxxoooooooxo
21xxxxxoxooooxo
22xxoxoxoooooxo
23xxoxxxoooooxx
24oxxxxooooxxxx
25xooxxooooxxxo
0.123091490979333
AR/MA
   0 1 2 3 4 5 6 7 8 9 10 11 12
0  x x x x x x x x x x x  x  x 
1  x x o x x x x x o x x  x  x 
2  x x o x o o x x o o o  x  x 
3  x o x x o o o x x o o  x  o 
4  x x x x o o o x x o o  x  o 
5  x o x x o o o x o o o  x  o 
6  x o o x x x x x o x o  x  o 
7  x x o x x o o x o o o  x  o 
8  x o x x x o x x o o o  x  o 
9  x x x x x x x x o x o  o  o 
10 x x o x x x x x o x o  o  o 
11 o x o x x o o o x o o  o  o 
12 x x x x o o o o o x x  o  o 
13 x x x x o o o o o o o  x  x 
14 x x x o o o o o o o o  x  o 
15 x x x x o o o x o o o  x  x 
16 x o o o o o x x o o o  x  o 
17 x o o o o o x o o o o  x  x 
18 x o o o o x o o o o x  x  x 
19 o o o o x x o o o o x  x  o 
20 o x x x o o o o o o o  x  o 
21 x x x x x o x o o o o  x  o 
22 x x o x o x o o o o o  x  o 
23 x x o x x x o o o o o  x  x 
24 o x x x x o o o o x x  x  x 
25 x o o x x o o o o x x  x  o 
In [104]:
par(bg = 'white')
# FIXME
m1 = arima(pow, order = c(0, 1, 1), seasonal = list(order = c(0, 1, 1), period = 12))
m1
tsdiag(m1, gof = 36)
# predict(m1, 24)
Call:
arima(x = pow, order = c(0, 1, 1), seasonal = list(order = c(0, 1, 1), period = 12))

Coefficients:
          ma1     sma1
      -0.4865  -0.9664
s.e.   0.0631   0.1179

sigma^2 estimated as 0.0003337:  log likelihood = 633.59,  aic = -1263.17
No description has been provided for this image
In [111]:
pow_ts = ts(pow, frequency = 12, start = c(2010, 1))
npts = 24
eotr = length(pow_ts)-npts
h = npts
freq = 12
# The order and seasonal following in combination gives a multiplicative seasonal model.
order = c(0, 1, 1)
seasonal = list(order = c(0, 1, 1), period = 12)
fixed = NULL
pow_fc_res = plot_arima_forecast_fig(
    da_ts=pow_ts, eotr=eotr, h=h, npts=npts, frequency=freq, 
    order=order, seasonal=seasonal, fixed=fixed, method='ML',
    include.mean=T, transform.pars=TRUE,
    main="Forecasts from ARIMA(0,1,1)(0,1,1)[12] for Power consumption",
    xlab="Year", ylab="Power", ylim=c(9.6, 10.1)
)
[1] 227
2027.917 ; 2032
No description has been provided for this image
In [44]:
pow_fc_tb = comb_forecast_res(pow_fc_res, pow_ts, eotr, freq)
pow_fc_tb
Forecast method: ARIMA(0,1,1)(0,1,1)[12]

Model Information:

Call:
arima(x = tr_da_ts, order = order, seasonal = seasonal, include.mean = include.mean, 
    fixed = fixed, method = method)

Coefficients:
          ma1     sma1
      -0.5018  -0.9721
s.e.   0.0658   0.1861

sigma^2 estimated as 0.0003399:  log likelihood = 569.22,  aic = -1134.43

Error measures:
                       ME       RMSE        MAE         MPE      MAPE      MASE
Training set 0.0005686413 0.01806016 0.01399088 0.005729905 0.1463576 0.4023562
                   ACF1
Training set 0.04081969

Forecasts:
         Point Forecast    Lo 80     Hi 80    Lo 95     Hi 95
Jan 2030       9.771751 9.747797  9.795706 9.735116  9.808386
Feb 2030       9.757840 9.731082  9.784598 9.716917  9.798764
Mar 2030       9.760923 9.731628  9.790219 9.716120  9.805727
Apr 2030       9.779608 9.747979  9.811238 9.731235  9.827981
May 2030       9.808923 9.775120  9.842725 9.757226  9.860619
Jun 2030       9.893825 9.857981  9.929670 9.839006  9.948645
Jul 2030       9.934974 9.897198  9.972750 9.877201  9.992748
Aug 2030       9.950545 9.910932  9.990159 9.889962 10.011129
Sep 2030       9.959941 9.918572 10.001311 9.896672 10.023211
Oct 2030       9.893905 9.850851  9.936959 9.828060  9.959750
Nov 2030       9.839844 9.795169  9.884519 9.771520  9.908168
Dec 2030       9.816089 9.769850  9.862328 9.745373  9.886806
Jan 2031       9.797054 9.748953  9.845154 9.723490  9.870617
Feb 2031       9.783142 9.733433  9.832851 9.707119  9.859165
Mar 2031       9.786225 9.734959  9.837492 9.707820  9.864631
Apr 2031       9.804911 9.752132  9.857689 9.724193  9.885629
May 2031       9.834225 9.779977  9.888473 9.751259  9.917191
Jun 2031       9.919128 9.863448  9.974807 9.833973 10.004282
Jul 2031       9.960276 9.903202 10.017351 9.872989 10.047564
Aug 2031       9.975848 9.917411 10.034284 9.886477 10.065218
Sep 2031       9.985244 9.925477 10.045011 9.893838 10.076649
Oct 2031       9.919207 9.858138  9.980276 9.825810 10.012604
Nov 2031       9.865146 9.802803  9.927490 9.769800  9.960492
Dec 2031       9.841391 9.777799  9.904984 9.744135  9.938648
A Time Series: 2 × 12
JanFebMarAprMayJunJulAugSepOctNovDec
20309.7717519.7578409.7609239.7796089.8089239.8938259.9349749.9505459.9599419.8939059.8398449.816089
20319.7970549.7831429.7862259.8049119.8342259.9191289.9602769.9758489.9852449.9192079.8651469.841391
A Time Series: 2 × 12
JanFebMarAprMayJunJulAugSepOctNovDec
20300.018691680.020879720.022859280.024680570.026376390.027969590.029476800.030910610.032280790.033595140.034859960.03608048
20310.037533130.038788050.040003630.041183350.042330200.043446780.044535380.045598000.046636420.047652210.048646790.04962145
A Time Series: 2 × 12
JanFebMarAprMayJunJulAugSepOctNovDec
2030 9.7696 9.7559 9.7804 9.8166 9.8245 9.8991 9.9229 9.9577 9.9685 9.9279 9.8715 9.8323
2031 9.8411 9.8128 9.8544 9.8625 9.9017 9.985710.028410.049910.0598 9.9722 9.9080 9.9060
A Time Series: 24 × 3
ForecastStd. ErrorActual
Jan 20309.7717510.01869168 9.7696
Feb 20309.7578400.02087972 9.7559
Mar 20309.7609230.02285928 9.7804
Apr 20309.7796080.02468057 9.8166
May 20309.8089230.02637639 9.8245
Jun 20309.8938250.02796959 9.8991
Jul 20309.9349740.02947680 9.9229
Aug 20309.9505450.03091061 9.9577
Sep 20309.9599410.03228079 9.9685
Oct 20309.8939050.03359514 9.9279
Nov 20309.8398440.03485996 9.8715
Dec 20309.8160890.03608048 9.8323
Jan 20319.7970540.03753313 9.8411
Feb 20319.7831420.03878805 9.8128
Mar 20319.7862250.04000363 9.8544
Apr 20319.8049110.04118335 9.8625
May 20319.8342250.04233020 9.9017
Jun 20319.9191280.04344678 9.9857
Jul 20319.9602760.0445353810.0284
Aug 20319.9758480.0455980010.0499
Sep 20319.9852440.0466364210.0598
Oct 20319.9192070.04765221 9.9722
Nov 20319.8651460.04864679 9.9080
Dec 20319.8413910.04962145 9.9060

2-7 (Equal-Weighted Index)

In [512]:
require(xts)
require(sandwich)
require(lmtest)
require(forecast)
In [513]:
find("read.table")
'package:utils'

Fit a time series model for CRSP equal-weighted index

  • Data are daily simple returns.
  • Test the effect of trading days using regression model without time series error.
  • Add time series error model by using ARIMA model. The order of ARIMA can be seen from some acf and pacf plots; Also can look at eacf plots and tables. After getting a starting point for the order parameter of arima function, we can trim the number of coefs by reducing element numbers in order, until we have all coefs close to be statistically significant.
  • The logic of determining the order in arima model is as following.
    • We see that the m1$residuals have some autocorrelation in acf. We need to take diff.
    • After taking one diff, we see that the diff(m1$residuals) have some autocorrelation in both pacf and acf. We take seasonal diff.
    • After taking one seasonal diff, we see that pacf has some strong seasonality. And acf has some autocorrelation. Let's look at effect of both regular diff and seasonal diff.
    • After taking both regular and seasonal diff, we see that the autocorrelation in both pacf and acf getting stronger. We probably don't want to do this.
    • So, we need to apply seasonal diff and model the decaying autocorrelation in the pacf. That means modeling the AR process.
  • Interesting observations.
    • The log(ew_rtn) shows medium momentum and serial correlation in pacf at lag-1. See plot_pacf_acf results.
In [516]:
da = read.table("../AFTS_sol/data/d-ibm3dxwkdays8008.txt", header = TRUE)
da[1:5,]
A data.frame: 5 × 12
yearmomdayibmvwewspMTWRF
<int><int><int><dbl><dbl><dbl><dbl><int><int><int><int><int>
1198012-0.029126-0.020089-0.011686-0.02019600100
2198013 0.016000-0.006510-0.011628-0.00510600010
3198014-0.001969 0.013735 0.015809 0.01235500001
4198017-0.003945 0.004368 0.007013 0.00272210000
5198018 0.067327 0.019340 0.014152 0.02003601000
In [517]:
ew = da$ew * 100
ew_ts = ts(ew, frequency = 252, start = c(1980, 1, 2))
plot(
    xts(ew, order.by = as.Date(paste(da$year, da$mom, da$day, sep = '-'))),
    type = 'l', main = '', xlab = 'date', ylab = 'ew'
)
No description has been provided for this image
In [539]:
plot_pacf_acf(ew, freq = 12, lag.max = 42)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [519]:
lg_ew = log(1+da$ew)
lg_ew_ts = ts(lg_ew, frequency = 252, start = c(1980, 1, 2))
plot(
    xts(lg_ew_ts, order.by = as.Date(paste(da$year, da$mom, da$day, sep = '-'))),
    type = 'l', main = '', xlab = 'date', ylab = 'lg(1+ew)'
)
No description has been provided for this image
In [540]:
plot_pacf_acf(lg_ew, freq = 12, lag.max = 42)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
  • Trading day effects are all significant.
In [116]:
Mo = da$M
Tu = da$T
We = da$W
Th = da$R
m1 = lm(ew ~ Mo + Tu + We + Th)
summary(m1)
Call:
lm(formula = ew ~ M + T + W + R)

Residuals:
     Min       1Q   Median       3Q      Max 
-10.2962  -0.3094   0.0533   0.3795  10.8319 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.22386    0.02155  10.389  < 2e-16 ***
M           -0.31734    0.03085 -10.286  < 2e-16 ***
T           -0.19778    0.03028  -6.532 6.94e-11 ***
W           -0.10185    0.03027  -3.365 0.000770 ***
R           -0.10294    0.03042  -3.384 0.000719 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.8234 on 7314 degrees of freedom
Multiple R-squared:  0.01618,	Adjusted R-squared:  0.01564 
F-statistic: 30.06 on 4 and 7314 DF,  p-value: < 2.2e-16
  • Trading day effects are still significant. So HAC estimator doesn't change the conclusion of weekday effects.
In [17]:
find("NeweyWest")
'package:sandwich'
In [117]:
coeftest(m1, NeweyWest(m1, lag = 12, prewhite = F))
t test of coefficients:

             Estimate Std. Error  t value  Pr(>|t|)    
(Intercept)  0.223862   0.018984  11.7924 < 2.2e-16 ***
M           -0.317340   0.029024 -10.9338 < 2.2e-16 ***
T           -0.197783   0.026948  -7.3395 2.375e-13 ***
W           -0.101854   0.026331  -3.8683 0.0001106 ***
R           -0.102943   0.025381  -4.0560 5.044e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  • Residuals have serial correlation.
    • Box-Ljung test tells us the null-hypothesis can be rejected.
In [118]:
Box.test(m1$residuals, lag = 12, type = 'Ljung')
	Box-Ljung test

data:  m1$residuals
X-squared = 514.96, df = 12, p-value < 2.2e-16
  • Residuals have some autocorrelations.
In [119]:
par(bg = 'white')
plot_time_fig(m1$residuals)
plot_time_fig(m1$residuals[1:200])
plot_time_fig(m1$residuals[(length(m1$residuals)-200):length(m1$residuals)])
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [122]:
par(mfrow = c(2, 1), bg = 'white')
pacf(m1$residuals)
acf(m1$residuals)
pacf(diff(m1$residuals))
acf(diff(m1$residuals))
pacf(diff(m1$residuals, 5))
acf(diff(m1$residuals, 5))
pacf(diff(diff(m1$residuals), 5))
acf(diff(diff(m1$residuals), 5))
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
  • Fit an ARIMA model with time series error.
In [59]:
ew_eacf_res <- perform_and_print_eacf(ew, ar.max = 25, ma.max = 12)
A data.frame: 26 × 13
0123456789101112
<I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>>
1 0.1991 0.0457 0.09133 0.054374 0.03882 5.56e-02 0.03096 0.02780 0.03645 0.027609 0.004854 0.06137 0.03047
2-0.0330-0.0827 0.05637-0.008304-0.01521 3.13e-02-0.01383-0.00505 0.01116 0.021850-0.000777 0.05525-0.01356
3-0.0796-0.3413 0.04200-0.010707-0.02214 2.35e-02-0.01466-0.01345 0.00726 0.001192-0.013286 0.04953-0.01297
4-0.2366-0.1846-0.18664-0.037721 0.00298-3.29e-03-0.01294 0.01206-0.00890-0.000875-0.015403 0.04869-0.00071
5-0.5000 0.0493-0.32938-0.066020-0.01221-2.66e-06-0.00280-0.00139-0.00883-0.001862-0.010022 0.01152 0.01163
6-0.4371-0.1815-0.36205-0.142458 0.02558-6.49e-03-0.00268 0.00162-0.00515-0.006756-0.014952 0.01954 0.00104
7-0.1738-0.3438-0.42397 0.155797-0.10697 4.26e-02 0.00701 0.00139-0.00703-0.004850-0.017060 0.01121 0.00826
8-0.3802-0.0588-0.29784-0.202874 0.04540 4.84e-02-0.00116 0.00208-0.01975 0.005105-0.008692 0.01378-0.01034
9-0.4783 0.1760-0.03593-0.153479 0.03060 4.32e-02 0.00780 0.06465-0.01597 0.002325-0.013141 0.00941-0.00838
10-0.4195 0.2650-0.03748-0.090770 0.14952-2.55e-01 0.03402 0.06760-0.04079-0.028994-0.007919 0.00119-0.00881
11 0.4903-0.0832-0.16157-0.186672 0.10171-2.80e-01 0.08032 0.04187-0.15953 0.021513-0.006925 0.00189 0.00126
12 0.1535-0.1568-0.33435-0.297189-0.16982-3.45e-01-0.16917-0.14890-0.14307-0.041255 0.003619 0.00257 0.00156
13-0.0354-0.2572 0.00564-0.450855 0.26370 1.88e-01-0.09841-0.17321-0.12558-0.027698 0.018525 0.00214-0.01098
14-0.1252-0.2557-0.00254-0.368854 0.13005 2.15e-01-0.19295-0.12754 0.02938-0.051806-0.007715-0.01742-0.18524
15-0.0882-0.2607-0.00773-0.253866 0.19894 2.00e-01-0.27774-0.15243-0.01056-0.109390 0.126849 0.03959-0.19531
16-0.0273-0.2508 0.01765-0.298093 0.23613 2.10e-01-0.21458-0.05457 0.09607-0.189322 0.168713 0.04839-0.20277
17 0.4257 0.4224 0.06380-0.108750 0.03701-1.99e-01 0.11547-0.13522-0.02931-0.211889-0.105417-0.12144-0.27727
18-0.5003 0.4423 0.13404-0.069572-0.01253-5.13e-02-0.01848-0.16813 0.01347-0.245807 0.013330 0.05827-0.27105
19 0.4965 0.4987 0.15593-0.196893-0.06260-5.48e-02 0.01833-0.01737-0.03233-0.240333 0.037863 0.01612-0.13779
20-0.4919 0.4729 0.13547-0.317312 0.29066-2.21e-02 0.02868-0.09510 0.00284-0.188563-0.078972-0.05367-0.09741
21 0.4248 0.3451 0.17017-0.017905-0.07052-1.03e-01 0.02755-0.03374-0.01763-0.194757-0.109101-0.04638 0.06374
22 0.3304 0.1048 0.16782-0.004319-0.05508-6.38e-02-0.04872-0.06977-0.11524-0.257878-0.064314-0.03350 0.01807
23 0.1085-0.0579 0.34436-0.000961-0.21443 2.26e-01-0.20265 0.21078 0.04499-0.238023 0.085021-0.00031-0.02534
24 0.0695-0.4345 0.34362-0.001231-0.17837-4.95e-02-0.11285 0.10954 0.08377-0.133928 0.098015 0.00314-0.09128
25-0.2373-0.0864-0.42230 0.083429-0.18267 6.07e-02-0.02557 0.19995 0.11316 0.046554 0.011300-0.04570-0.08013
26-0.4522 0.0342-0.34910-0.068288-0.15929-4.90e-02-0.04732 0.17984 0.02677 0.041395 0.008066-0.06806-0.08910
A matrix: 26 × 13 of type chr
0123456789101112
0xxxxxxxxxxoxx
1xxxooxoooooxo
2xxxooxoooooxo
3xxxxoooooooxo
4xxxxooooooooo
5xxxxxoooooooo
6xxxxxxooooooo
7xxxxxxooooooo
8xxxxxxoxooooo
9xxxxxxxxxxooo
10xxxxxxxxxoooo
11xxxxxxxxxxooo
12xxoxxxxxxxooo
13xxoxxxxxxxoox
14xxoxxxxxoxxxx
15xxoxxxxxxxxxx
16xxxxxxxxxxxxx
17xxxxoxoxoxoxx
18xxxxxxooxxxox
19xxxxxoxxoxxxx
20xxxoxxxxoxxxx
21xxxoxxxxxxxxo
22xxxoxxxxxxxox
23xxxoxxxxxxxox
24xxxxxxxxxxoxx
25xxxxxxxxxxoxx
0.0233778260111891
AR/MA
   0 1 2 3 4 5 6 7 8 9 10 11 12
0  x x x x x x x x x x o  x  x 
1  x x x o o x o o o o o  x  o 
2  x x x o o x o o o o o  x  o 
3  x x x x o o o o o o o  x  o 
4  x x x x o o o o o o o  o  o 
5  x x x x x o o o o o o  o  o 
6  x x x x x x o o o o o  o  o 
7  x x x x x x o o o o o  o  o 
8  x x x x x x o x o o o  o  o 
9  x x x x x x x x x x o  o  o 
10 x x x x x x x x x o o  o  o 
11 x x x x x x x x x x o  o  o 
12 x x o x x x x x x x o  o  o 
13 x x o x x x x x x x o  o  x 
14 x x o x x x x x o x x  x  x 
15 x x o x x x x x x x x  x  x 
16 x x x x x x x x x x x  x  x 
17 x x x x o x o x o x o  x  x 
18 x x x x x x o o x x x  o  x 
19 x x x x x o x x o x x  x  x 
20 x x x o x x x x o x x  x  x 
21 x x x o x x x x x x x  x  o 
22 x x x o x x x x x x x  o  x 
23 x x x o x x x x x x x  o  x 
24 x x x x x x x x x x o  x  x 
25 x x x x x x x x x x o  x  x 
In [56]:
par(bg = 'white')
m2 = arima(
    ew, order = c(2, 0, 2),
    seasonal = list(order = c(1, 0, 0), period = 5),
    xreg = da[, 8:11]
)
m2
tsdiag(m2, gof = 20)
Call:
arima(x = ew, order = c(2, 0, 2), seasonal = list(order = c(1, 0, 0), period = 5), 
    xreg = da[, 8:11])

Coefficients:
         ar1     ar2      ma1      ma2    sar1  intercept        M        T
      0.5409  0.3211  -0.3431  -0.4035  -0.041     0.2242  -0.3171  -0.1982
s.e.  0.0950  0.0731   0.0928   0.0580   0.013     0.0239   0.0267   0.0280
            W        R
      -0.1058  -0.0987
s.e.   0.0282   0.0263

sigma^2 estimated as 0.6412:  log likelihood = -8759.03,  aic = 17538.07
No description has been provided for this image
In [58]:
par(bg = 'white')
m3 = arima(
    ew, order = c(2, 0, 3), 
    seasonal = list(order = c(1, 0, 0), period = 5), 
    xreg = da[, 8:11]
)
m3
tsdiag(m3, gof = 20)
Call:
arima(x = ew, order = c(2, 0, 3), seasonal = list(order = c(1, 0, 0), period = 5), 
    xreg = da[, 8:11])

Coefficients:
         ar1     ar2      ma1      ma2      ma3     sar1  intercept        M
      0.4950  0.3675  -0.2966  -0.4397  -0.0099  -0.0412     0.2242  -0.3169
s.e.  0.1635  0.1548   0.1639   0.1209   0.0296   0.0131     0.0240   0.0267
            T        W        R
      -0.1983  -0.1056  -0.0986
s.e.   0.0280   0.0282   0.0263

sigma^2 estimated as 0.6412:  log likelihood = -8758.99,  aic = 17539.97
No description has been provided for this image
In [24]:
par(bg = 'white')
m4 = arima(
    ew, order = c(4, 0, 4), 
    seasonal = list(order = c(1, 0, 0), period = 5), 
    xreg = da[, 8:11]
)
m4
tsdiag(m4, gof = 20)
Call:
arima(x = ew, order = c(4, 0, 4), seasonal = list(order = c(1, 0, 0), period = 5), 
    xreg = da[, 8:11])

Coefficients:
         ar1      ar2     ar3     ar4     ma1     ma2      ma3      ma4
      0.0926  -0.3079  0.8712  0.0879  0.1076  0.3207  -0.7429  -0.2188
s.e.  0.1167   0.0213  0.0516  0.0930  0.1155  0.0311   0.0638   0.0756
         sar1  intercept        M        T        W        R
      -0.0115     0.2241  -0.3161  -0.1979  -0.1066  -0.0977
s.e.   0.0138     0.0242   0.0268   0.0283   0.0285   0.0264

sigma^2 estimated as 0.6367:  log likelihood = -8733.3,  aic = 17496.59
No description has been provided for this image
In [25]:
par(bg = 'white')
m4 = arima(
    ew, order = c(3, 0, 3), 
    seasonal = list(order = c(1, 0, 0), period = 5), 
    xreg = da[, 8:11]
)
m4
tsdiag(m4, gof = 20)
Warning message in arima(ew, order = c(3, 0, 3), seasonal = list(order = c(1, 0, :
“possible convergence problem: optim gave code = 1”
Call:
arima(x = ew, order = c(3, 0, 3), seasonal = list(order = c(1, 0, 0), period = 5), 
    xreg = da[, 8:11])

Coefficients:
          ar1     ar2     ar3     ma1      ma2      ma3     sar1  intercept
      -0.5192  0.8231  0.3977  0.7205  -0.7018  -0.4755  -0.0350     0.2219
s.e.   0.1057  0.0431  0.0761  0.1032   0.0637   0.0585   0.0139     0.0239
            M        T        W        R
      -0.3145  -0.1955  -0.1019  -0.0952
s.e.   0.0267   0.0281   0.0283   0.0263

sigma^2 estimated as 0.64:  log likelihood = -8752.36,  aic = 17530.72
No description has been provided for this image
In [126]:
par(bg = 'white')
m5 = arima(
    ew, order = c(2, 0, 2), 
#     seasonal = list(order = c(0, 0, 1), period = 5), 
    xreg = da[, 8:11]
)
m5
tsdiag(m5, gof = 20)
Call:
arima(x = ew, order = c(2, 0, 2), xreg = da[, 8:11])

Coefficients:
         ar1     ar2      ma1      ma2  intercept        M        T        W
      0.6226  0.2504  -0.4251  -0.3515     0.2244  -0.3181  -0.1988  -0.1053
s.e.  0.0948  0.0685   0.0931   0.0552     0.0243   0.0275   0.0291   0.0293
            R
      -0.0994
s.e.   0.0271

sigma^2 estimated as 0.6421:  log likelihood = -8764.1,  aic = 17546.19
No description has been provided for this image
In [125]:
par(bg = 'white')
m5 = arima(
    ew, order = c(2, 0, 2), 
    seasonal = list(order = c(0, 0, 1), period = 5), 
    xreg = da[, 8:11]
)
m5
tsdiag(m5, gof = 20)
Call:
arima(x = ew, order = c(2, 0, 2), seasonal = list(order = c(0, 0, 1), period = 5), 
    xreg = da[, 8:11])

Coefficients:
         ar1     ar2      ma1      ma2     sma1  intercept        M        T
      0.5399  0.3229  -0.3421  -0.4050  -0.0426     0.2241  -0.3171  -0.1981
s.e.  0.0945  0.0734   0.0923   0.0584   0.0133     0.0239   0.0267   0.0279
            W        R
      -0.1054  -0.0983
s.e.   0.0281   0.0262

sigma^2 estimated as 0.6412:  log likelihood = -8758.85,  aic = 17537.69
No description has been provided for this image
In [128]:
par(bg = 'white')
m5 = arima(
    ew, order = c(2, 0, 2), 
    seasonal = list(order = c(1, 0, 0), period = 5), 
    xreg = da[, 8:11]
)
m5
tsdiag(m5, gof = 20)
Call:
arima(x = ew, order = c(2, 0, 2), seasonal = list(order = c(1, 0, 0), period = 5), 
    xreg = da[, 8:11])

Coefficients:
         ar1     ar2      ma1      ma2    sar1  intercept        M        T
      0.5409  0.3211  -0.3431  -0.4035  -0.041     0.2242  -0.3171  -0.1982
s.e.  0.0950  0.0731   0.0928   0.0580   0.013     0.0239   0.0267   0.0280
            W        R
      -0.1058  -0.0987
s.e.   0.0282   0.0263

sigma^2 estimated as 0.6412:  log likelihood = -8759.03,  aic = 17538.07
No description has been provided for this image
In [127]:
par(bg = 'white')
m5 = arima(
    ew, order = c(2, 0, 2), 
    seasonal = list(order = c(1, 0, 1), period = 5), 
    xreg = da[, 8:11]
)
m5
tsdiag(m5, gof = 20)
Call:
arima(x = ew, order = c(2, 0, 2), seasonal = list(order = c(1, 0, 1), period = 5), 
    xreg = da[, 8:11])

Coefficients:
         ar1     ar2     ma1      ma2    sar1     sma1  intercept        M
      0.5554  0.3206  -0.358  -0.4060  0.2901  -0.3341     0.2244  -0.3173
s.e.  0.0924  0.0738   0.090   0.0592  0.1727   0.1702     0.0239   0.0264
            T        W        R
      -0.1977  -0.1062  -0.0981
s.e.   0.0276   0.0277   0.0260

sigma^2 estimated as 0.641:  log likelihood = -8757.93,  aic = 17537.86
No description has been provided for this image

Forecast using the model

In [7]:
help(auto.arima)

Test auto.arima with a subset of time series data

In [17]:
da[6560:6570,]
A data.frame: 11 × 12
yearmomdayibmvwewspMTWRF
<int><int><int><dbl><dbl><dbl><dbl><int><int><int><int><int>
656020051223 0.003124 0.000981 0.003363 0.00042600001
656120051227-0.005870-0.009806-0.007569-0.00955301000
656220051228 0.000603 0.002857 0.003299 0.00129700100
656320051229-0.007707-0.002343 0.000072-0.00298100010
656420051230-0.002427-0.004415 0.000053-0.00488700001
65652006 1 3-0.001703 0.016428 0.010981 0.01643001000
65662006 1 4-0.001340 0.005531 0.007697 0.00367300100
65672006 1 5 0.006711-0.000332 0.002920 0.00001600010
65682006 1 6 0.029697 0.009813 0.009032 0.00939900001
65692006 1 9-0.014361 0.004303 0.007009 0.00365610000
65702006 110 0.004061 0.000978 0.004278-0.00035701000
In [19]:
sub_ew_ts = ts(ew[6565:length(ew)], frequency = 252, start = c(2006, 1, 3))
sub_da = da[6565:length(ew),]
length(sub_ew_ts)
755
In [23]:
sub_ts_fm <- auto.arima(
    sub_ew_ts,
    d = 0,
    D = 0,
    max.p = 2,
    max.q = 2,
    max.P = 1,
    max.Q = 0,
    max.order = 5,
#     nmodels = 10,
    seasonal = TRUE,
    method = "ML",
    allowmean = TRUE,
    xreg = as.matrix(sub_da[, 8:11]),
#     stepwise = FALSE,
#     parallel = TRUE,
#     num.cores = 6
)
sub_ts_fm
Series: sub_ew_ts 
Regression with ARIMA(2,0,2) errors 

Coefficients:
          ar1      ar2     ma1     ma2        M       T        W       R
      -0.9976  -0.6214  1.0519  0.5740  -0.2183  0.0709  -0.0023  -0.060
s.e.   0.2445   0.2018  0.2520  0.2289   0.1147  0.1108   0.1099   0.111

sigma^2 = 2.013:  log likelihood = -1331.43
AIC=2680.87   AICc=2681.11   BIC=2722.51
In [25]:
class(sub_ts_fm); attributes(sub_ts_fm)
  1. 'forecast_ARIMA'
  2. 'ARIMA'
  3. 'Arima'
$names
  1. 'coef'
  2. 'sigma2'
  3. 'var.coef'
  4. 'mask'
  5. 'loglik'
  6. 'aic'
  7. 'arma'
  8. 'residuals'
  9. 'call'
  10. 'series'
  11. 'code'
  12. 'n.cond'
  13. 'nobs'
  14. 'model'
  15. 'bic'
  16. 'aicc'
  17. 'xreg'
  18. 'x'
  19. 'fitted'
$class
  1. 'forecast_ARIMA'
  2. 'ARIMA'
  3. 'Arima'
In [29]:
h = 20
sub_ts_fc_res <- forecast(sub_ts_fm, h = h, xreg = as.matrix(sub_da[(dim(sub_da)[1]-h):(dim(sub_da)[1]), 8:11]))
summary(sub_ts_fc_res)
Forecast method: Regression with ARIMA(2,0,2) errors

Model Information:
Series: sub_ew_ts 
Regression with ARIMA(2,0,2) errors 

Coefficients:
          ar1      ar2     ma1     ma2        M       T        W       R
      -0.9976  -0.6214  1.0519  0.5740  -0.2183  0.0709  -0.0023  -0.060
s.e.   0.2445   0.2018  0.2520  0.2289   0.1147  0.1108   0.1099   0.111

sigma^2 = 2.013:  log likelihood = -1331.43
AIC=2680.87   AICc=2681.11   BIC=2722.51

Error measures:
                     ME     RMSE       MAE      MPE     MAPE      MASE
Training set 0.01357602 1.411296 0.8930034 106.3844 138.8498 0.7143814
                   ACF1
Training set 0.01716401

Forecasts:
         Point Forecast     Lo 80    Hi 80     Lo 95    Hi 95
2008.996   -0.116092511 -1.934400 1.702215 -2.896953 2.664768
2009.000   -0.149994315 -1.970978 1.670990 -2.934949 2.634960
2009.004    0.203478305 -1.626850 2.033807 -2.595768 3.002724
2009.008   -0.171110322 -2.005563 1.663343 -2.976664 2.634443
2009.012   -0.211340702 -2.045811 1.623129 -3.016920 2.594239
2009.016    0.170234654 -1.665515 2.005985 -2.637303 2.977772
2009.020   -0.105806788 -1.943019 1.731405 -2.915580 2.703966
2009.024   -0.018528507 -1.855994 1.818937 -2.828689 2.791632
2009.028    0.022914938 -1.814612 1.860442 -2.787340 2.833170
2009.032   -0.266953625 -2.104796 1.570888 -3.077690 2.543783
2009.036    0.105149309 -1.732857 1.943155 -2.705838 2.916137
2009.040   -0.006317078 -1.844326 1.831692 -2.817309 2.804675
2009.044   -0.077339654 -1.915387 1.760708 -2.888391 2.733712
2009.048    0.019764592 -1.818336 1.857865 -2.791368 2.830897
2009.052   -0.227265507 -2.065378 1.610847 -3.038416 2.583885
2009.056    0.067512392 -1.770601 1.905626 -2.743640 2.878665
2009.060    0.006568932 -1.831555 1.844693 -2.804599 2.817737
2009.063   -0.006801287 -1.844932 1.831329 -2.817979 2.804377
2009.067   -0.217068216 -2.055199 1.621063 -3.028247 2.594110
2009.071    0.073847161 -1.764285 1.911979 -2.737333 2.885027
2009.075   -0.006087489 -1.844221 1.832046 -2.817271 2.805096
In [31]:
par(bg = 'white')
plot(sub_ts_fc_res)
No description has been provided for this image
In [32]:
sub_ts_fc_res$mean
A Time Series:
7299
-0.116092511465873
7300
-0.149994315460839
7301
0.203478304950891
7302
-0.171110322048078
7303
-0.211340702172188
7304
0.170234654498533
7305
-0.105806788188471
7306
-0.0185285070907785
7307
0.022914938280641
7308
-0.266953625499702
7309
0.105149309488776
7310
-0.00631707820380557
7311
-0.0773396544195852
7312
0.0197645917869954
7313
-0.227265507085973
7314
0.0675123922873163
7315
0.00656893192937669
7316
-0.00680128731368499
7317
-0.217068215706301
7318
0.0738471605783947
7319
-0.00608748867555768
In [33]:
sub_ts_fc_res$lower; sub_ts_fc_res$upper
A Time Series: 21 × 2
80%95%
2008.996-1.934400-2.896953
2009.000-1.970978-2.934949
2009.004-1.626850-2.595768
2009.008-2.005563-2.976664
2009.012-2.045811-3.016920
2009.016-1.665515-2.637303
2009.020-1.943019-2.915580
2009.024-1.855994-2.828689
2009.028-1.814612-2.787340
2009.032-2.104796-3.077690
2009.036-1.732857-2.705838
2009.040-1.844326-2.817309
2009.044-1.915387-2.888391
2009.048-1.818336-2.791368
2009.052-2.065378-3.038416
2009.056-1.770601-2.743640
2009.060-1.831555-2.804599
2009.063-1.844932-2.817979
2009.067-2.055199-3.028247
2009.071-1.764285-2.737333
2009.075-1.844221-2.817271
A Time Series: 21 × 2
80%95%
2008.9961.7022152.664768
2009.0001.6709902.634960
2009.0042.0338073.002724
2009.0081.6633432.634443
2009.0121.6231292.594239
2009.0162.0059852.977772
2009.0201.7314052.703966
2009.0241.8189372.791632
2009.0281.8604422.833170
2009.0321.5708882.543783
2009.0361.9431552.916137
2009.0401.8316922.804675
2009.0441.7607082.733712
2009.0481.8578652.830897
2009.0521.6108472.583885
2009.0561.9056262.878665
2009.0601.8446932.817737
2009.0631.8313292.804377
2009.0671.6210632.594110
2009.0711.9119792.885027
2009.0751.8320462.805096

Plot forecast results from auto.arima model with a subset of time series

In [41]:
help(stopifnot)
In [112]:
npts = 20
eotr = length(sub_ew_ts)-npts
h = npts
freq = 252
xreg = as.matrix(sub_da[, 8:11])
sub_ew_fc_res = plot_auto_arima_forecast_fig(
    da_ts=sub_ew_ts, eotr=eotr, h=h, npts=npts, frequency=freq, 
    xreg=xreg,
    main="Forecasts from ARIMA(1,1,0)\nfor CRSP Equal-Weighted Index", 
    xlab="Year", ylab="CRSP Equal-Weighted Index"# , ylim=c(5, 11)
)
[1] 734
2008.833 ; 2008.996
No description has been provided for this image
In [49]:
sub_ew_fc_tb = comb_forecast_res(sub_ew_fc_res, sub_ew_ts, eotr, freq)
sub_ew_fc_tb
Forecast method: Regression with ARIMA(1,1,0) errors

Model Information:
Series: tr_da_ts 
Regression with ARIMA(1,1,0) errors 

Coefficients:
          ar1    drift     xreg
      -0.4319  -0.0011  -0.1894
s.e.   0.0343   0.0444   0.1196

sigma^2 = 2.983:  log likelihood = -1441.2
AIC=2890.39   AICc=2890.45   BIC=2908.79

Error measures:
                        ME     RMSE      MAE      MPE     MAPE     MASE
Training set -0.0001707146 1.722408 1.026185 128.2297 323.7154 0.850973
                   ACF1
Training set -0.1714507

Forecasts:
         Point Forecast     Lo 80     Hi 80      Lo 95     Hi 95
2008.917   -1.298624463 -3.512011 0.9147619  -4.683707  2.086458
2008.921    0.849334056 -1.696299 3.3949670  -3.043876  4.742544
2008.925   -0.079878948 -3.124575 2.9648171  -4.736340  4.576582
2008.929    0.130539853 -3.260069 3.5211483  -5.054948  5.316028
2008.933    0.145715474 -3.590326 3.8817573  -5.568067  5.859498
2008.937    0.219419115 -3.819953 4.2587911  -5.958267  6.397105
2008.940    0.186057742 -4.140531 4.5126467  -6.430889  6.803004
2008.944    0.198936766 -4.394866 4.7927393  -6.826678  7.224551
2008.948    0.002474672 -4.844685 4.8496339  -7.410616  7.415565
2008.952    0.193378414 -4.894185 5.2809422  -7.587379  7.974136
2008.956    0.191186694 -5.126069 5.5084420  -7.940853  8.323227
2008.960    0.190603831 -5.346763 5.7279709  -8.278068  8.659276
2008.964    0.189326118 -5.559757 5.9384089  -8.603137  8.981789
2008.968   -0.001021811 -5.954285 5.9522417  -9.105753  9.103709
2008.972    0.187241279 -5.963433 6.3379156  -9.219403  9.593886
2008.976    0.186190032 -6.155751 6.5281314  -9.512972  9.885352
2008.980    0.185114609 -6.342493 6.7127217  -9.797999 10.168228
2008.984   -0.005320687 -6.713456 6.7028151 -10.264529 10.253888
2008.988    0.182980137 -6.700952 7.0669120 -10.345085 10.711045
2008.992    0.181912593 -6.873436 7.2372616 -10.608313 10.972138
A Time Series:
  1. -1.29862446268606
  2. 0.849334055523345
  3. -0.0798789475238652
  4. 0.13053985253886
  5. 0.145715473588767
  6. 0.21941911505262
  7. 0.186057742363957
  8. 0.198936765953585
  9. 0.00247467231699205
  10. 0.193378414374947
  11. 0.19118669434306
  12. 0.190603831226609
  13. 0.18932611758486
  14. -0.0010218112634669
  15. 0.187241279177729
  16. 0.186190032326165
  17. 0.18511460931776
  18. -0.0053206869971778
  19. 0.182980136650425
  20. 0.181912593173573
A Time Series:
  1. 1.72711452400449
  2. 1.98636795601448
  3. 2.37578891756174
  4. 2.64570582602999
  5. 2.91524892767082
  6. 3.15193872378606
  7. 3.37605527893742
  8. 3.5845631759321
  9. 3.7822584655444
  10. 3.96984714598476
  11. 4.14907632789653
  12. 4.32083048132734
  13. 4.48603312615497
  14. 4.64535623790657
  15. 4.79939669948165
  16. 4.94864311134307
  17. 5.09351889303796
  18. 5.23438612895216
  19. 5.37156054784085
  20. 5.50531804706904
A Time Series:
  1. 1.496
  2. -2.2343
  3. 2.695
  4. 3.8066
  5. -1.5286
  6. 2.2634
  7. -2.7784
  8. 1.9321
  9. -2.1021
  10. 4.7713
  11. 1.1627
  12. -1.1401
  13. 0.3764
  14. -1.2094
  15. -0.8332
  16. 0.5254
  17. 1.1629
  18. -1.6514
  19. 2.1692
  20. 3.6731
A Time Series: 20 × 3
ForecastStd. ErrorActual
2008.917-1.2986244631.727115 1.4960
2008.921 0.8493340561.986368-2.2343
2008.925-0.0798789482.375789 2.6950
2008.929 0.1305398532.645706 3.8066
2008.933 0.1457154742.915249-1.5286
2008.937 0.2194191153.151939 2.2634
2008.940 0.1860577423.376055-2.7784
2008.944 0.1989367663.584563 1.9321
2008.948 0.0024746723.782258-2.1021
2008.952 0.1933784143.969847 4.7713
2008.956 0.1911866944.149076 1.1627
2008.960 0.1906038314.320830-1.1401
2008.964 0.1893261184.486033 0.3764
2008.968-0.0010218114.645356-1.2094
2008.972 0.1872412794.799397-0.8332
2008.976 0.1861900324.948643 0.5254
2008.980 0.1851146095.093519 1.1629
2008.984-0.0053206875.234386-1.6514
2008.988 0.1829801375.371561 2.1692
2008.992 0.1819125935.505318 3.6731
In [62]:
class(sub_ew_fc_res); attributes(sub_ew_fc_res);
class(sub_ew_fc_res$model); attributes(sub_ew_fc_res$model);
'forecast'
$names
  1. 'method'
  2. 'model'
  3. 'level'
  4. 'mean'
  5. 'lower'
  6. 'upper'
  7. 'x'
  8. 'series'
  9. 'fitted'
  10. 'residuals'
$class
'forecast'
  1. 'forecast_ARIMA'
  2. 'ARIMA'
  3. 'Arima'
$names
  1. 'coef'
  2. 'sigma2'
  3. 'var.coef'
  4. 'mask'
  5. 'loglik'
  6. 'aic'
  7. 'arma'
  8. 'residuals'
  9. 'call'
  10. 'series'
  11. 'code'
  12. 'n.cond'
  13. 'nobs'
  14. 'model'
  15. 'xreg'
  16. 'bic'
  17. 'aicc'
  18. 'x'
  19. 'fitted'
$class
  1. 'forecast_ARIMA'
  2. 'ARIMA'
  3. 'Arima'
In [59]:
sub_ew_fc_res$model
Series: tr_da_ts 
Regression with ARIMA(1,1,0) errors 

Coefficients:
          ar1    drift     xreg
      -0.4319  -0.0011  -0.1894
s.e.   0.0343   0.0444   0.1196

sigma^2 = 2.983:  log likelihood = -1441.2
AIC=2890.39   AICc=2890.45   BIC=2908.79

All data takes too long time to run auto.arima

Dummy example for auto.arima

In [50]:
# Assuming 'forecast' package is installed and loaded
library(forecast)

# Simulate some time series data
set.seed(123)
ts_data <- ts(rnorm(100), frequency = 12, start = c(2000, 1))

# Simulate exogenous regressor data for model fitting
xreg_train <- matrix(rnorm(200), ncol = 2) # Two exogenous regressors

# Fit an ARIMA model with exogenous regressors
# fit <- auto.arima(ts_data, xreg = xreg_train)
fit <- auto.arima(ts_data, xreg = xreg_train)

# Simulate future values of the exogenous regressors for forecasting
xreg_future <- matrix(rnorm(24), ncol = 2) # Two exogenous regressors for 12 future periods

# Forecast using the model and future values of exogenous regressors
forecast_result <- forecast(fit, xreg = xreg_future)

# Plot the forecast
par(bg = 'white')
plot(forecast_result)
No description has been provided for this image
In [57]:
npts = 24
eotr = length(ts_data)-npts
h = npts
freq = 12
xreg = xreg_train
sim_data_fc_res = plot_auto_arima_forecast_fig(
    da_ts=ts_data, eotr=eotr, h=h, npts=npts, frequency=freq, 
    xreg=xreg,
    main="Forecasts from ARIMA(0,0,0) for Simulated Data", 
    xlab="Year", ylab="Simulated Data"# , ylim=c(5, 11)
)
[1] 76
2004.25 ; 2008.333
No description has been provided for this image

2-8 (S&P)

  • Data are daily simple returns.
  • Interesting observations.
    • The diff(log(sp)) shows medium mean-reverting and serial correlation in pacf at lag-2. See plot_pacf_acf results.
In [141]:
require(xts)
In [145]:
da = read.table("../AFTS_sol/data/d-ibm3dxwkdays8008.txt", header = TRUE)
dim(da); da[1:5,]
  1. 7319
  2. 12
A data.frame: 5 × 12
yearmomdayibmvwewspMTWRF
<int><int><int><dbl><dbl><dbl><dbl><int><int><int><int><int>
1198012-0.029126-0.020089-0.011686-0.02019600100
2198013 0.016000-0.006510-0.011628-0.00510600010
3198014-0.001969 0.013735 0.015809 0.01235500001
4198017-0.003945 0.004368 0.007013 0.00272210000
5198018 0.067327 0.019340 0.014152 0.02003601000
In [524]:
sp = da$sp * 100
sp_ts = ts(sp, frequency = 252, start = c(1980, 1, 2))
plot(xts(sp, order.by = as.Date(paste(da$year, da$mom, da$day, sep = '-'))),
    type = 'l', main = '', xlab = 'date', ylab = 'sp')
lg_sp = log(1+da$sp)
lg_sp_ts = ts(lg_sp, frequency = 252, start = c(1980, 1, 2))
plot(xts(lg_sp, order.by = as.Date(paste(da$year, da$mom, da$day, sep = '-'))),
    type = 'l', main = '', xlab = 'date', ylab = 'lg(sp)')
No description has been provided for this image
No description has been provided for this image
In [522]:
plot_pacf_acf(sp, freq = 12, lag.max = 42)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [538]:
plot_pacf_acf(lg_sp, freq = 12, lag.max = 42)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image

2-8 (a)

  • No Friday effect.
In [159]:
plot(
    xts(sp, order.by = as.Date(paste(da$year, da$mom, da$day, sep = '-'))),
    type = 'l', main = '', xlab = 'date', ylab = 'sp'
)
No description has been provided for this image
In [162]:
Mo = da$M
Tu = da$T
We = da$W
Th = da$R
Fr = da$F
sp_m1 = lm(sp ~ Mo + Tu + We + Th + Fr + 0)
summary(sp_m1);
Call:
lm(formula = sp ~ M + T + W + R + F + 0)

Residuals:
     Min       1Q   Median       3Q      Max 
-20.4627  -0.5214   0.0142   0.5379  11.5842 

Coefficients:
   Estimate Std. Error t value Pr(>|t|)  
M -0.004214   0.029944  -0.141   0.8881  
T  0.074036   0.028855   2.566   0.0103 *
W  0.063973   0.028836   2.219   0.0265 *
R  0.006432   0.029128   0.221   0.8252  
F  0.032896   0.029228   1.125   0.2604  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.117 on 7314 degrees of freedom
Multiple R-squared:  0.001753,	Adjusted R-squared:  0.00107 
F-statistic: 2.568 on 5 and 7314 DF,  p-value: 0.02501
In [163]:
Box.test(sp_m1$residuals, lag = 12, type = 'Ljung');
	Box-Ljung test

data:  sp_m1$residuals
X-squared = 67.025, df = 12, p-value = 1.149e-09
In [220]:
sp_m1 = lm(sp ~ 1 + T + W)
summary(sp_m1);
Call:
lm(formula = sp ~ 1 + T + W)

Residuals:
     Min       1Q   Median       3Q      Max 
-20.4788  -0.5215   0.0150   0.5353  11.5681 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept)  0.01195    0.01699   0.703   0.4819  
T            0.06209    0.03348   1.854   0.0637 .
W            0.05203    0.03347   1.555   0.1201  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.117 on 7316 degrees of freedom
Multiple R-squared:  0.0006394,	Adjusted R-squared:  0.0003662 
F-statistic: 2.341 on 2 and 7316 DF,  p-value: 0.09635

2-8 (b)

  • There are some serial correlation in the residuals.
In [164]:
plot_pacf_acf(sp_m1$residuals, lag.max = 20, main = 'obs')
plot_pacf_acf(diff(sp_m1$residuals), lag.max = 20, main = 'diff(obs)')
plot_pacf_acf(diff(sp_m1$residuals, 5), lag.max = 20, main = "diff(obs, 5)")
plot_pacf_acf(diff(diff(sp_m1$residuals), 5), lag.max = 20, main = "diff(diff(obs), 5)")
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [169]:
par(bg = 'white')
sp_m2 = arima(
    sp, order = c(0, 0, 1),
#     seasonal = list(order = c(1, 0, 0), period = 5),
    xreg = da[, 8:11]
)
sp_m2
tsdiag(sp_m2, gof = 20)
Call:
arima(x = sp, order = c(0, 0, 1), xreg = da[, 8:11])

Coefficients:
          ma1  intercept        M       T       W        R
      -0.0191     0.0329  -0.0371  0.0411  0.0311  -0.0265
s.e.   0.0125     0.0292   0.0422  0.0411  0.0411   0.0416

sigma^2 estimated as 1.246:  log likelihood = -11190.07,  aic = 22392.13
No description has been provided for this image
In [170]:
par(bg = 'white')
sp_m2 = arima(
    sp, order = c(1, 0, 0),
#     seasonal = list(order = c(1, 0, 0), period = 5),
    xreg = da[, 8:11]
)
sp_m2
tsdiag(sp_m2, gof = 20)
Call:
arima(x = sp, order = c(1, 0, 0), xreg = da[, 8:11])

Coefficients:
          ar1  intercept        M       T       W        R
      -0.0167     0.0329  -0.0371  0.0411  0.0312  -0.0269
s.e.   0.0117     0.0292   0.0422  0.0411  0.0410   0.0416

sigma^2 estimated as 1.246:  log likelihood = -11190.21,  aic = 22392.42
No description has been provided for this image
In [171]:
par(bg = 'white')
sp_m2 = arima(
    sp, order = c(2, 0, 0),
#     seasonal = list(order = c(1, 0, 0), period = 5),
    xreg = da[, 8:11]
)
sp_m2
tsdiag(sp_m2, gof = 20)
Call:
arima(x = sp, order = c(2, 0, 0), xreg = da[, 8:11])

Coefficients:
          ar1      ar2  intercept        M       T       W        R
      -0.0178  -0.0627     0.0320  -0.0363  0.0426  0.0305  -0.0238
s.e.   0.0117   0.0117     0.0292   0.0421  0.0422  0.0423   0.0415

sigma^2 estimated as 1.241:  log likelihood = -11175.83,  aic = 22365.66
No description has been provided for this image
In [172]:
par(bg = 'white')
sp_m2 = arima(
    sp, order = c(2, 0, 0),
    seasonal = list(order = c(1, 0, 0), period = 5),
    xreg = da[, 8:11]
)
sp_m2
tsdiag(sp_m2, gof = 20)
Call:
arima(x = sp, order = c(2, 0, 0), seasonal = list(order = c(1, 0, 0), period = 5), 
    xreg = da[, 8:11])

Coefficients:
          ar1      ar2     sar1  intercept        M       T       W        R
      -0.0182  -0.0630  -0.0120     0.0320  -0.0363  0.0428  0.0303  -0.0234
s.e.   0.0117   0.0117   0.0117     0.0289   0.0417  0.0418  0.0419   0.0411

sigma^2 estimated as 1.241:  log likelihood = -11175.31,  aic = 22366.61
No description has been provided for this image
In [174]:
par(bg = 'white')
sp_m2 = arima(
    sp, order = c(3, 0, 1),
    xreg = da[, 8:11]
)
sp_m2
tsdiag(sp_m2, gof = 20)
Call:
arima(x = sp, order = c(3, 0, 1), xreg = da[, 8:11])

Coefficients:
          ar1      ar2      ar3     ma1  intercept        M       T       W
      -0.4878  -0.0708  -0.0167  0.4708     0.0321  -0.0363  0.0426  0.0302
s.e.   0.2230   0.0136   0.0196  0.2225     0.0292   0.0422  0.0420  0.0421
            R
      -0.0238
s.e.   0.0416

sigma^2 estimated as 1.241:  log likelihood = -11175.06,  aic = 22368.12
No description has been provided for this image
In [195]:
par(bg = 'white')
sp_m2 = arima(
    sp, order = c(4, 0, 0),
    xreg = da[, 8:11]
)
sp_m2
tsdiag(sp_m2, gof = 20)
Call:
arima(x = sp, order = c(4, 0, 0), xreg = da[, 8:11])

Coefficients:
          ar1      ar2     ar3      ar4  intercept        M       T       W
      -0.0176  -0.0651  0.0016  -0.0399     0.0322  -0.0367  0.0426  0.0300
s.e.   0.0117   0.0117  0.0117   0.0117     0.0291   0.0425  0.0419  0.0421
            R
      -0.0243
s.e.   0.0419

sigma^2 estimated as 1.239:  log likelihood = -11169.97,  aic = 22357.95
No description has been provided for this image
In [196]:
par(bg = 'white')
sp_m2 = arima(
    sp, order = c(4, 0, 0),
    fixed = c(NA, NA, 0, NA, 0, 0, 0, 0, 0),
    xreg = da[, 8:11]
)
sp_m2
tsdiag(sp_m2, gof = 20)
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“some AR parameters were fixed: setting transform.pars = FALSE”
Call:
arima(x = sp, order = c(4, 0, 0), xreg = da[, 8:11], fixed = c(NA, NA, 0, NA, 
    0, 0, 0, 0, 0))

Coefficients:
          ar1      ar2  ar3      ar4  intercept  M  T  W  R
      -0.0166  -0.0643    0  -0.0388          0  0  0  0  0
s.e.   0.0117   0.0117    0   0.0117          0  0  0  0  0

sigma^2 estimated as 1.242:  log likelihood = -11177.18,  aic = 22360.37
No description has been provided for this image
In [198]:
par(bg = 'white')
sp_m2 = arima(
    sp, order = c(5, 0, 0),
    fixed = c(NA, NA, 0, NA, 0, 0, 0, 0, 0, 0),
    xreg = da[, 8:11]
)
sp_m2
tsdiag(sp_m2, gof = 20)
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“some AR parameters were fixed: setting transform.pars = FALSE”
Call:
arima(x = sp, order = c(5, 0, 0), xreg = da[, 8:11], fixed = c(NA, NA, 0, NA, 
    0, 0, 0, 0, 0, 0))

Coefficients:
          ar1      ar2  ar3      ar4  ar5  intercept  M  T  W  R
      -0.0166  -0.0643    0  -0.0388    0          0  0  0  0  0
s.e.   0.0117   0.0117    0   0.0117    0          0  0  0  0  0

sigma^2 estimated as 1.242:  log likelihood = -11177.18,  aic = 22360.37
No description has been provided for this image
In [ ]:

In [200]:
par(bg = 'white')
sp_m3 = arima(
    sp, order = c(2, 0, 0),
    xreg = da[, 8:11]
)
sp_m3
tsdiag(sp_m3, gof = 20)
Call:
arima(x = sp, order = c(2, 0, 0), xreg = da[, 8:11])

Coefficients:
          ar1      ar2  intercept        M       T       W        R
      -0.0178  -0.0627     0.0320  -0.0363  0.0426  0.0305  -0.0238
s.e.   0.0117   0.0117     0.0292   0.0421  0.0422  0.0423   0.0415

sigma^2 estimated as 1.241:  log likelihood = -11175.83,  aic = 22365.66
No description has been provided for this image
In [201]:
par(bg = 'white')
sp_m3 = arima(
    sp, order = c(2, 0, 2),
    xreg = da[, 8:11]
)
sp_m3
tsdiag(sp_m3, gof = 20)
Call:
arima(x = sp, order = c(2, 0, 2), xreg = da[, 8:11])

Coefficients:
         ar1     ar2      ma1      ma2  intercept        M       T       W
      0.0737  0.3057  -0.0899  -0.3717     0.0322  -0.0361  0.0426  0.0304
s.e.  0.1258  0.1043   0.1225   0.1014     0.0290   0.0423  0.0423  0.0424
            R
      -0.0242
s.e.   0.0417

sigma^2 estimated as 1.24:  log likelihood = -11171.52,  aic = 22361.04
No description has been provided for this image
In [203]:
par(bg = 'white')
sp_m3 = arima(
    sp, order = c(2, 0, 2),
    fixed = c(0, NA, 0, NA, 0, 0, 0, 0, 0),
    xreg = da[, 8:11]
)
sp_m3
tsdiag(sp_m3, gof = 20)
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“some AR parameters were fixed: setting transform.pars = FALSE”
Call:
arima(x = sp, order = c(2, 0, 2), xreg = da[, 8:11], fixed = c(0, NA, 0, NA, 
    0, 0, 0, 0, 0))

Coefficients:
      ar1     ar2  ma1      ma2  intercept  M  T  W  R
        0  0.2655    0  -0.3324          0  0  0  0  0
s.e.    0  0.1054    0   0.1028          0  0  0  0  0

sigma^2 estimated as 1.243:  log likelihood = -11180.13,  aic = 22364.27
No description has been provided for this image
In [ ]:

In [ ]:

In [213]:
sp_m4 = arima(sp, order = c(2, 0, 2), xreg = da[, 8:12], include.mean = FALSE)
sp_m4;
par(bg = 'white')
tsdiag(sp_m4, gof = 20)
Call:
arima(x = sp, order = c(2, 0, 2), xreg = da[, 8:12], include.mean = FALSE)

Coefficients:
         ar1     ar2      ma1      ma2        M       T       W       R       F
      0.0737  0.3057  -0.0899  -0.3717  -0.0039  0.0748  0.0626  0.0080  0.0322
s.e.  0.1258  0.1043   0.1225   0.1014   0.0297  0.0286  0.0286  0.0289  0.0290

sigma^2 estimated as 1.24:  log likelihood = -11171.52,  aic = 22361.04
No description has been provided for this image
  • Adding T and W, b/c they show significance in linear model.
In [215]:
sp_m4 = arima(sp, order = c(2, 0, 2), xreg = da[, c(9, 10, 12)], include.mean = FALSE,
    fixed = c(0, NA, 0, NA, NA, NA, NA))
sp_m4;
par(bg = 'white')
tsdiag(sp_m4, gof = 20)
Box.test(sp_m4$residuals, lag = 10, type = 'Ljung')
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“some AR parameters were fixed: setting transform.pars = FALSE”
Call:
arima(x = sp, order = c(2, 0, 2), xreg = da[, c(9, 10, 12)], include.mean = FALSE, 
    fixed = c(0, NA, 0, NA, NA, NA, NA))

Coefficients:
      ar1     ar2  ma1      ma2       T       W       F
        0  0.2736    0  -0.3413  0.0751  0.0619  0.0321
s.e.    0  0.1041    0   0.1014  0.0287  0.0286  0.0291

sigma^2 estimated as 1.24:  log likelihood = -11173.17,  aic = 22356.34
	Box-Ljung test

data:  sp_m4$residuals
X-squared = 16.368, df = 10, p-value = 0.08957
No description has been provided for this image
In [ ]:

2-9 (IBM)

  • Data are daily simple returns.
  • There is almost no serial correlation. See plot_pacf_acf results.
In [217]:
require(xts)
In [525]:
da = read.table("../AFTS_sol/data/d-ibm3dxwkdays8008.txt", header = TRUE)
da[1:5,]
A data.frame: 5 × 12
yearmomdayibmvwewspMTWRF
<int><int><int><dbl><dbl><dbl><dbl><int><int><int><int><int>
1198012-0.029126-0.020089-0.011686-0.02019600100
2198013 0.016000-0.006510-0.011628-0.00510600010
3198014-0.001969 0.013735 0.015809 0.01235500001
4198017-0.003945 0.004368 0.007013 0.00272210000
5198018 0.067327 0.019340 0.014152 0.02003601000
In [526]:
ibm = da$ibm * 100
plot(xts(ibm, order.by = as.Date(paste(da$year, da$mom, da$day, sep = '-'))),
    type = 'l', main = '', xlab = 'date', ylab = 'ibm')
lg_ibm = log(1+da$ibm)
plot(xts(lg_ibm, order.by = as.Date(paste(da$year, da$mom, da$day, sep = '-'))),
    type = 'l', main = '', xlab = 'date', ylab = 'lg(ibm)')
No description has been provided for this image
No description has been provided for this image
In [535]:
plot_pacf_acf(ibm, freq = 12, lag.max = 42)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [536]:
plot_pacf_acf(lg_ibm, freq = 12, lag.max = 42)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image

2-9 (a)

  • No Friday effect.
In [223]:
Mo = da$M
Tu = da$T
We = da$W
Th = da$R
Fr = da$F
ibm_m1 = lm(ibm ~ Mo + Tu + We + Th + Fr + 0)
summary(ibm_m1)
Call:
lm(formula = ibm ~ M + T + W + R + F + 0)

Residuals:
     Min       1Q   Median       3Q      Max 
-23.1629  -0.9290  -0.0036   0.8840  13.1619 

Coefficients:
   Estimate Std. Error t value Pr(>|t|)    
M  0.199941   0.047850   4.178 2.97e-05 ***
T  0.143942   0.046109   3.122   0.0018 ** 
W -0.036134   0.046079  -0.784   0.4330    
R  0.001708   0.046546   0.037   0.9707    
F -0.059021   0.046706  -1.264   0.2064    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.785 on 7314 degrees of freedom
Multiple R-squared:  0.004006,	Adjusted R-squared:  0.003325 
F-statistic: 5.884 on 5 and 7314 DF,  p-value: 1.966e-05
In [225]:
Box.test(ibm_m1$residuals, lag = 12, type = 'Ljung')
	Box-Ljung test

data:  ibm_m1$residuals
X-squared = 16.845, df = 12, p-value = 0.1555

2-9 (b)

  • There is no much serial correlation in the residuals.
In [231]:
lag.max = 20
par(mfrow = c(2, 1), bg = 'white')
pacf(ibm_m1$residuals, lag.max =lag.max)
acf(ibm_m1$residuals, lag.max =lag.max)
pacf(diff(ibm_m1$residuals), lag.max =lag.max)
acf(diff(ibm_m1$residuals), lag.max =lag.max)
pacf(diff(ibm_m1$residuals, 5), lag.max =lag.max)
acf(diff(ibm_m1$residuals, 5), lag.max =lag.max)
pacf(diff(diff(ibm_m1$residuals), 5), lag.max =lag.max)
acf(diff(diff(ibm_m1$residuals), 5), lag.max =lag.max)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image

2-9 (c)

In [228]:
ibm_m2 = arima(ibm, order = c(0, 0, 1), xreg = da[, 8:12], include.mean = FALSE)
ibm_m2;
par(bg = 'white')
tsdiag(ibm_m2, gof = 35)
Call:
arima(x = ibm, order = c(0, 0, 1), xreg = da[, 8:12], include.mean = FALSE)

Coefficients:
          ma1       M       T        W       R        F
      -0.0321  0.2000  0.1439  -0.0361  0.0017  -0.0590
s.e.   0.0118  0.0478  0.0461   0.0461  0.0465   0.0467

sigma^2 estimated as 3.179:  log likelihood = -14618.23,  aic = 29248.46
No description has been provided for this image
In [229]:
Box.test(ibm_m2$residuals, lag = 12, type = 'Ljung')
	Box-Ljung test

data:  ibm_m2$residuals
X-squared = 9.4174, df = 12, p-value = 0.6669
In [234]:
ibm_m2 = arima(ibm, order = c(0, 0, 1), xreg = da[, c(8, 9)], include.mean = TRUE)
ibm_m2;
par(bg = 'white')
tsdiag(ibm_m2, gof = 35)
Call:
arima(x = ibm, order = c(0, 0, 1), xreg = da[, c(8, 9)], include.mean = TRUE)

Coefficients:
          ma1  intercept      M       T
      -0.0323    -0.0311  0.231  0.1750
s.e.   0.0118     0.0262  0.055  0.0535

sigma^2 estimated as 3.18:  log likelihood = -14618.63,  aic = 29245.27
No description has been provided for this image
In [233]:
ibm_m2 = arima(ibm, order = c(0, 0, 1), xreg = da[, c(8, 9)], include.mean = FALSE)
ibm_m2;
par(bg = 'white')
tsdiag(ibm_m2, gof = 35)
Call:
arima(x = ibm, order = c(0, 0, 1), xreg = da[, c(8, 9)], include.mean = FALSE)

Coefficients:
          ma1       M       T
      -0.0322  0.1999  0.1439
s.e.   0.0118  0.0478  0.0461

sigma^2 estimated as 3.18:  log likelihood = -14619.33,  aic = 29244.65
No description has been provided for this image

2-10 (Aaa and Baa Bond Yields)

  • pp10: Test statistics for skewness and kurtosis.
  • They are all significant.
In [237]:
require(fBasics)
Loading required package: fBasics


Attaching package: ‘fBasics’


The following objects are masked from ‘package:TSA’:

    kurtosis, skewness


In [235]:
da1 = read.table("../AFTS_sol/data/w-Aaa.txt", header = FALSE)
da2 = read.table("../AFTS_sol/data/w-Baa.txt", header = FALSE)
da1[1:5,]; da2[1:5,];
A data.frame: 5 × 4
V1V2V3V4
<int><int><int><dbl>
119621 54.43
219621124.42
319621194.42
419621264.41
519622 24.42
A data.frame: 5 × 4
V1V2V3V4
<int><int><int><dbl>
119621 55.11
219621125.09
319621195.08
419621265.08
519622 25.07
In [238]:
Aaa = da1[, 4]
Baa = da2[, 4]
apply(cbind(Aaa, Baa), 2, basicStats)
ts = skewness(Aaa) / sqrt(6 / length(Aaa))
cat("ts =", ts, "\n")
ps = (1 - pnorm(ts)) * 2
cat("ps =", ps, "\n")
tk = kurtosis(Aaa) / sqrt(24 / length(Aaa))
cat("tk =", tk, "\n")
pk = (1 - pnorm(tk)) * 2
cat("pk =", pk, "\n")
ts = skewness(Baa) / sqrt(6 / length(Baa))
cat("ts =", ts, "\n")
ps = (1 - pnorm(ts)) * 2
cat("ps =", ps, "\n")
tk = kurtosis(Baa) / sqrt(24 / length(Baa))
cat("tk =", tk, "\n")
pk = (1 - pnorm(tk)) * 2
cat("pk =", pk, "\n")
$Aaa
A data.frame: 16 × 1
X..newX..i
<dbl>
nobs 2467.000000
NAs 0.000000
Minimum 4.190000
Maximum 15.850000
1. Quartile 5.985000
3. Quartile 8.930000
Mean 7.830109
Median 7.540000
Sum19316.880000
SE Mean 0.048697
LCL Mean 7.734618
UCL Mean 7.925601
Variance 5.850323
Stdev 2.418744
Skewness 0.857092
Kurtosis 0.578605
$Baa
A data.frame: 16 × 1
X..newX..i
<dbl>
nobs 2467.000000
NAs 0.000000
Minimum 4.780000
Maximum 17.290000
1. Quartile 6.990000
3. Quartile 10.200000
Mean 8.847122
Median 8.350000
Sum21825.850000
SE Mean 0.054704
LCL Mean 8.739852
UCL Mean 8.954392
Variance 7.382486
Stdev 2.717073
Skewness 0.929779
Kurtosis 0.760896
ts = 17.37946 
ps = 0 
tk = 5.866262 
pk = 4.457306e-09 
ts = 18.85335 
ps = 0 
tk = 7.714438 
pk = 1.221245e-14 

2-11 (Aaa and Baa Bond Yields)

  • Data are weekly Bond Yields.
  • Interesting observation.
    • The diff(aaa) or diff(log(aaa)) shows medium to strong momentum at lag-1 in pacf and acf, but not in longer lags. See plot_pacf_acf results.
In [345]:
da1 = read.table("../AFTS_sol/data/w-Aaa.txt", header = FALSE)
da1[1:5,];
A data.frame: 5 × 4
V1V2V3V4
<int><int><int><dbl>
119621 54.43
219621124.42
319621194.42
419621264.41
519622 24.42
In [346]:
aaa = da1[,4]
aaa_ts = ts(aaa, frequency = 52, start = c(1962, 1))
lg_aaa = log(aaa)
lg_aaa_ts = ts(lg_aaa, frequency = 52, start = c(1962, 1))

Fitting solution

In [253]:
sub_sz = 100
plot_time_fig(lg_aaa_ts, main = "AAA Bond", xlab = "Year", ylab = "lg(Bond Weekly Yield)")
plot_time_fig(lg_aaa_ts[1:sub_sz], main = "AAA Bond", xlab = "Year", ylab = "lg(Bond Weekly Yield)")
plot_time_fig(lg_aaa_ts[(length(lg_aaa_ts)-sub_sz):length(lg_aaa_ts)], main = "AAA Bond", xlab = "Year", ylab = "lg(Bond Weekly Yield)")
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [243]:
sub_sz = 100
plot_time_fig(aaa_ts, main = "AAA Bond", xlab = "Year", ylab = "Bond Weekly Yield")
plot_time_fig(aaa_ts[1:sub_sz], main = "AAA Bond", xlab = "Year", ylab = "Bond Weekly Yield")
plot_time_fig(aaa_ts[(length(aaa_ts)-sub_sz):length(aaa_ts)], main = "AAA Bond", xlab = "Year", ylab = "Bond Weekly Yield")
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [250]:
# lag.max = 52
# par(mfrow = c(2, 1), bg = 'white')
# pacf(aaa, lag.max = lag.max)
# acf(aaa, lag.max = lag.max)
# pacf(diff(aaa), lag.max = lag.max)
# acf(diff(aaa), lag.max = lag.max)
# pacf(diff(aaa, 4), lag.max = lag.max)
# acf(diff(aaa, 4), lag.max = lag.max)
# pacf(diff(aaa, 13), lag.max = lag.max)
# acf(diff(aaa, 13), lag.max = lag.max)
# pacf(diff(aaa, 26), lag.max = lag.max)
# acf(diff(aaa, 26), lag.max = lag.max)
# pacf(diff(diff(aaa), 26), lag.max = lag.max)
# acf(diff(diff(aaa), 26), lag.max = lag.max)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [543]:
plot_pacf_acf(aaa, freq = 26, lag.max = 52)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [544]:
plot_pacf_acf(lg_aaa, freq = 26, lag.max = 52)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [ ]:

In [256]:
par(bg = 'white')
aaa_m1 = arima(
    aaa, order = c(0, 1, 1), 
    seasonal = list(order = c(0, 1, 1), period = 26)
)
aaa_m1
tsdiag(aaa_m1, gof = 52)
Call:
arima(x = aaa, order = c(0, 1, 1), seasonal = list(order = c(0, 1, 1), period = 26))

Coefficients:
         ma1     sma1
      0.3674  -0.9807
s.e.  0.0182   0.0111

sigma^2 estimated as 0.00823:  log likelihood = 2351.57,  aic = -4699.13
No description has been provided for this image
In [259]:
par(bg = 'white')
aaa_m2 = arima(
    aaa, order = c(1, 1, 0), 
    seasonal = list(order = c(1, 1, 0), period = 26)
)
aaa_m2
tsdiag(aaa_m2, gof = 52)
Call:
arima(x = aaa, order = c(1, 1, 0), seasonal = list(order = c(1, 1, 0), period = 26))

Coefficients:
         ar1     sar1
      0.3826  -0.5206
s.e.  0.0187   0.0174

sigma^2 estimated as 0.01159:  log likelihood = 1971.92,  aic = -3939.84
No description has been provided for this image
In [258]:
par(bg = 'white')
aaa_m2 = arima(
    aaa, order = c(1, 1, 1), 
    seasonal = list(order = c(1, 1, 1), period = 26)
)
aaa_m2
tsdiag(aaa_m2, gof = 52)
Call:
arima(x = aaa, order = c(1, 1, 1), seasonal = list(order = c(1, 1, 1), period = 26))

Coefficients:
         ar1     ma1     sar1     sma1
      0.2695  0.1155  -0.0004  -0.9803
s.e.  0.0660  0.0701   0.0216   0.0118

sigma^2 estimated as 0.008178:  log likelihood = 2359.52,  aic = -4711.04
No description has been provided for this image
In [260]:
par(bg = 'white')
aaa_m2 = arima(
    aaa, order = c(1, 1, 1), 
    seasonal = list(order = c(0, 1, 1), period = 26)
)
aaa_m2
tsdiag(aaa_m2, gof = 52)
Call:
arima(x = aaa, order = c(1, 1, 1), seasonal = list(order = c(0, 1, 1), period = 26))

Coefficients:
         ar1     ma1     sma1
      0.2694  0.1156  -0.9804
s.e.  0.0660  0.0701   0.0114

sigma^2 estimated as 0.008178:  log likelihood = 2359.52,  aic = -4713.04
No description has been provided for this image
In [261]:
par(bg = 'white')
aaa_m2 = arima(
    aaa, order = c(2, 1, 1), 
    seasonal = list(order = c(0, 1, 1), period = 26)
)
aaa_m2
tsdiag(aaa_m2, gof = 52)
Call:
arima(x = aaa, order = c(2, 1, 1), seasonal = list(order = c(0, 1, 1), period = 26))

Coefficients:
          ar1     ar2     ma1     sma1
      -0.2492  0.1869  0.6398  -0.9798
s.e.   0.1162  0.0514  0.1127   0.0112

sigma^2 estimated as 0.008156:  log likelihood = 2363.16,  aic = -4718.31
No description has been provided for this image
In [262]:
par(bg = 'white')
aaa_m2 = arima(
    aaa, order = c(3, 1, 1), 
    seasonal = list(order = c(0, 1, 1), period = 26)
)
aaa_m2
tsdiag(aaa_m2, gof = 52)
Call:
arima(x = aaa, order = c(3, 1, 1), seasonal = list(order = c(0, 1, 1), period = 26))

Coefficients:
         ar1      ar2     ar3      ma1     sma1
      0.5016  -0.1024  0.0872  -0.1211  -0.9819
s.e.  0.2284   0.0884  0.0205   0.2292   0.0117

sigma^2 estimated as 0.008117:  log likelihood = 2367.75,  aic = -4725.5
No description has been provided for this image
In [263]:
par(bg = 'white')
aaa_m2 = arima(
    aaa, order = c(4, 1, 1), 
    seasonal = list(order = c(0, 1, 1), period = 26)
)
aaa_m2
tsdiag(aaa_m2, gof = 52)
Warning message in sqrt(diag(x$var.coef)):
“NaNs produced”
Call:
arima(x = aaa, order = c(4, 1, 1), seasonal = list(order = c(0, 1, 1), period = 26))

Coefficients:
         ar1     ar2     ar3     ar4     ma1     sma1
      0.1604  0.0277  0.0713  0.0197  0.2204  -0.9820
s.e.     NaN     NaN  0.0038     NaN     NaN   0.0118

sigma^2 estimated as 0.008117:  log likelihood = 2367.61,  aic = -4723.22
No description has been provided for this image
In [265]:
par(bg = 'white')
aaa_m2 = arima(
    aaa, order = c(9, 1, 1), 
    seasonal = list(order = c(0, 1, 1), period = 26)
)
aaa_m2
tsdiag(aaa_m2, gof = 52)
Call:
arima(x = aaa, order = c(9, 1, 1), seasonal = list(order = c(0, 1, 1), period = 26))

Coefficients:
         ar1      ar2     ar3      ar4     ar5      ar6      ar7     ar8
      0.6287  -0.1528  0.0978  -0.0187  0.0431  -0.0462  -0.0413  0.0404
s.e.  0.3546   0.1361  0.0312   0.0392  0.0242   0.0288   0.0282  0.0282
          ar9      ma1     sma1
      -0.0611  -0.2502  -0.9809
s.e.   0.0205   0.3556   0.0114

sigma^2 estimated as 0.00804:  log likelihood = 2379.85,  aic = -4737.71
No description has been provided for this image
In [266]:
par(bg = 'white')
aaa_m2 = arima(
    aaa, order = c(9, 1, 1), 
    seasonal = list(order = c(0, 1, 1), period = 26),
    fixed = c(NA, 0, NA, 0, NA, NA, NA, NA, NA, 0, NA)
)
aaa_m2
tsdiag(aaa_m2, gof = 52)
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“some AR parameters were fixed: setting transform.pars = FALSE”
Call:
arima(x = aaa, order = c(9, 1, 1), seasonal = list(order = c(0, 1, 1), period = 26), 
    fixed = c(NA, 0, NA, 0, NA, NA, NA, NA, NA, 0, NA))

Coefficients:
         ar1  ar2    ar3  ar4     ar5      ar6      ar7     ar8      ar9  ma1
      0.3604    0  0.066    0  0.0413  -0.0353  -0.0539  0.0296  -0.0570    0
s.e.  0.0189    0  0.019    0  0.0203   0.0216   0.0216  0.0216   0.0202    0
         sma1
      -0.9805
s.e.   0.0115

sigma^2 estimated as 0.008068:  log likelihood = 2375.89,  aic = -4735.79
No description has been provided for this image
In [267]:
par(bg = 'white')
aaa_m2 = arima(
    aaa, order = c(9, 1, 1), 
    seasonal = list(order = c(0, 1, 1), period = 26),
    fixed = c(NA, 0, NA, 0, NA, 0, NA, 0, NA, 0, NA)
)
aaa_m2
tsdiag(aaa_m2, gof = 52)
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“some AR parameters were fixed: setting transform.pars = FALSE”
Call:
arima(x = aaa, order = c(9, 1, 1), seasonal = list(order = c(0, 1, 1), period = 26), 
    fixed = c(NA, 0, NA, 0, NA, 0, NA, 0, NA, 0, NA))

Coefficients:
         ar1  ar2     ar3  ar4     ar5  ar6      ar7  ar8      ar9  ma1
      0.3577    0  0.0656    0  0.0313    0  -0.0559    0  -0.0490    0
s.e.  0.0189    0  0.0190    0  0.0190    0   0.0190    0   0.0189    0
         sma1
      -0.9807
s.e.   0.0115

sigma^2 estimated as 0.008084:  log likelihood = 2373.45,  aic = -4734.91
No description has been provided for this image
In [273]:
par(bg = 'white')
aaa_m2 = arima(aaa, order = c(9, 1, 1))
aaa_m2
tsdiag(aaa_m2, gof = 52)
Call:
arima(x = aaa, order = c(9, 1, 1))

Coefficients:
         ar1      ar2     ar3      ar4     ar5      ar6      ar7     ar8
      0.6627  -0.1655  0.1000  -0.0194  0.0384  -0.0426  -0.0427  0.0415
s.e.  0.3576   0.1364  0.0314   0.0396  0.0244   0.0284   0.0278  0.0286
          ar9      ma1
      -0.0601  -0.2866
s.e.   0.0206   0.3586

sigma^2 estimated as 0.007928:  log likelihood = 2465.16,  aic = -4910.32
No description has been provided for this image
In [276]:
par(bg = 'white')
aaa_m2 = arima(
    aaa, order = c(9, 1, 0),
    fixed = c(NA, 0, NA, 0, 0, 0, NA, 0, NA)
)
aaa_m2
tsdiag(aaa_m2, gof = 52)
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“some AR parameters were fixed: setting transform.pars = FALSE”
Call:
arima(x = aaa, order = c(9, 1, 0), fixed = c(NA, 0, NA, 0, 0, 0, NA, 0, NA))

Coefficients:
         ar1  ar2     ar3  ar4  ar5  ar6      ar7  ar8      ar9
      0.3580    0  0.0700    0    0    0  -0.0532    0  -0.0466
s.e.  0.0187    0  0.0188    0    0    0   0.0188    0   0.0187

sigma^2 estimated as 0.007976:  log likelihood = 2457.81,  aic = -4907.63
No description has been provided for this image
In [ ]:

Alternative solution

In [268]:
plot(
    xts(aaa, order.by = as.Date(paste(da1[, 1], da1[, 2], da1[, 3], sep = '-'))),
    type = 'l', main = '', xlab = 'date', ylab = 'Aaa'
)
No description has been provided for this image
In [270]:
cat("order =", ar(diff(aaa), method = 'mle')$order, "\n")
order = 9 
In [271]:
aaa_al_m1 = arima(aaa, order = c(9, 1, 0))
aaa_al_m1;
par(bg = 'white')
tsdiag(aaa_al_m1, gof = 52)
Call:
arima(x = aaa, order = c(9, 1, 0))

Coefficients:
         ar1      ar2     ar3     ar4     ar5      ar6      ar7     ar8
      0.3772  -0.0579  0.0843  0.0054  0.0392  -0.0314  -0.0531  0.0274
s.e.  0.0201   0.0215  0.0215  0.0216  0.0215   0.0215   0.0215  0.0215
          ar9
      -0.0592
s.e.   0.0201

sigma^2 estimated as 0.00793:  log likelihood = 2464.86,  aic = -4911.72
No description has been provided for this image
In [291]:
aaa_al_m1 = arima(
    aaa, order = c(9, 1, 0),
    seasonal = list(order = c(1, 0, 0), period = 13)
)
aaa_al_m1;
par(bg = 'white')
tsdiag(aaa_al_m1, gof = 52)
Call:
arima(x = aaa, order = c(9, 1, 0), seasonal = list(order = c(1, 0, 0), period = 13))

Coefficients:
         ar1      ar2     ar3     ar4     ar5      ar6      ar7     ar8
      0.3771  -0.0580  0.0844  0.0053  0.0393  -0.0316  -0.0529  0.0275
s.e.  0.0201   0.0215  0.0215  0.0216  0.0215   0.0216   0.0215  0.0215
          ar9     sar1
      -0.0589  -0.0024
s.e.   0.0202   0.0205

sigma^2 estimated as 0.00793:  log likelihood = 2464.87,  aic = -4909.73
No description has been provided for this image
In [277]:
aaa_al_m2 = arima(aaa, order = c(2, 1, 3))
aaa_al_m2;
par(bg = 'white')
tsdiag(aaa_al_m2, gof = 52)
Call:
arima(x = aaa, order = c(2, 1, 3))

Coefficients:
         ar1      ar2      ma1     ma2     ma3
      1.4382  -0.6681  -1.0606  0.2126  0.2229
s.e.  0.1677   0.1017   0.1655  0.0600  0.0358

sigma^2 estimated as 0.007976:  log likelihood = 2457.81,  aic = -4905.62
No description has been provided for this image
In [288]:
aaa_al_m2 = arima(
    aaa, order = c(2, 1, 3),
    seasonal = list(order = c(1, 0, 0), period = 26)
)
aaa_al_m2;
par(bg = 'white')
tsdiag(aaa_al_m2, gof = 52)
Call:
arima(x = aaa, order = c(2, 1, 3), seasonal = list(order = c(1, 0, 0), period = 26))

Coefficients:
         ar1      ar2      ma1     ma2     ma3    sar1
      1.4202  -0.6566  -1.0427  0.2074  0.2218  0.0023
s.e.  0.1676   0.1082   0.1659  0.0632  0.0353  0.0206

sigma^2 estimated as 0.007976:  log likelihood = 2457.81,  aic = -4903.62
No description has been provided for this image

Forecast

In [405]:
npts = 26
eotr = length(aaa_ts)-npts
h = npts
freq = 52
order = c(9,1,0)
fixed = NULL
seasonal = NULL
aaa_fc_res = plot_arima_forecast_fig(
    da_ts=aaa_ts, eotr=eotr, h=h, npts=npts, frequency=freq, 
    order=order, seasonal=seasonal, fixed=fixed, method='ML', 
    include.mean=TRUE, transform.pars=TRUE,
    main="Forecasts from ARIMA(9,1,0) for Aaa weekly bond yields", 
    xlab="Year", ylab="Aaa weekly bond yields", ylim=c(4, 8)
)
[1] 2440
2008.423 ; 2009.442
No description has been provided for this image
No description has been provided for this image
In [283]:
# aaa_fc_tb = comb_forecast_res(aaa_fc_res, aaa_ts, eotr, freq)
# aaa_fc_tb
Forecast method: ARIMA(9,1,0)

Model Information:

Call:
arima(x = tr_da_ts, order = order, seasonal = seasonal, include.mean = include.mean, 
    fixed = fixed, method = method)

Coefficients:
         ar1      ar2     ar3      ar4     ar5      ar6      ar7     ar8
      0.3998  -0.0872  0.1069  -0.0305  0.0836  -0.0631  -0.0200  0.0062
s.e.  0.0202   0.0218  0.0219   0.0220  0.0219   0.0220   0.0219  0.0218
          ar9
      -0.0366
s.e.   0.0203

sigma^2 estimated as 0.007577:  log likelihood = 2494.55,  aic = -4971.1

Error measures:
                       ME       RMSE        MAE         MPE      MAPE
Training set 0.0004752123 0.08702582 0.05640702 0.006786893 0.6917595
                   MASE          ACF1
Training set 0.07420383 -0.0007418544

Forecasts:
         Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
2008.942       6.207367 6.095816 6.318917 6.036764 6.377969
2008.962       6.245664 6.053766 6.437563 5.952180 6.539148
2008.981       6.283379 6.030786 6.535972 5.897072 6.669686
2009.000       6.284329 5.976738 6.591920 5.813909 6.754749
2009.019       6.289618 5.932881 6.646355 5.744036 6.835200
2009.038       6.286249 5.881784 6.690713 5.667674 6.904823
2009.058       6.267942 5.820319 6.715565 5.583362 6.952523
2009.077       6.259608 5.774415 6.744802 5.517569 7.001648
2009.096       6.248974 5.729201 6.768747 5.454050 7.043898
2009.115       6.240264 5.689523 6.791004 5.397979 7.082548
2009.135       6.235576 5.656606 6.814546 5.350118 7.121034
2009.154       6.230780 5.625430 6.836131 5.304976 7.156585
2009.173       6.229189 5.599089 6.859289 5.265534 7.192844
2009.192       6.228524 5.574816 6.882233 5.228764 7.228285
2009.212       6.228147 5.551861 6.904432 5.193857 7.262436
2009.231       6.229018 5.531003 6.927033 5.161497 7.296540
2009.250       6.229685 5.510556 6.948814 5.129872 7.329499
2009.269       6.230454 5.490795 6.970113 5.099243 7.361665
2009.288       6.231238 5.471554 6.990922 5.069402 7.393074
2009.308       6.231713 5.452463 7.010963 5.039953 7.423473
2009.327       6.232172 5.433800 7.030544 5.011167 7.453177
2009.346       6.232437 5.415350 7.049524 4.982810 7.482064
2009.365       6.232556 5.397150 7.067963 4.954913 7.510200
2009.385       6.232639 5.379291 7.085986 4.927557 7.537720
2009.404       6.232622 5.361692 7.103553 4.900649 7.564596
2009.423       6.232587 5.344419 7.120754 4.874251 7.590922
A Time Series:
  1. 6.20736655719426
  2. 6.24566440620478
  3. 6.28337907629132
  4. 6.2843292268527
  5. 6.28961777033179
  6. 6.28624851922467
  7. 6.26794223063653
  8. 6.25960830867983
  9. 6.24897423756542
  10. 6.24026376349615
  11. 6.23557584923931
  12. 6.23078046674665
  13. 6.22918899621492
  14. 6.22852446989816
  15. 6.22814662901021
  16. 6.22901817876502
  17. 6.22968504832111
  18. 6.23045371472518
  19. 6.23123800093802
  20. 6.23171307644446
  21. 6.23217201001091
  22. 6.23243689662713
  23. 6.23255633090181
  24. 6.23263853121758
  25. 6.23262240593548
  26. 6.23258655524976
A Time Series:
  1. 0.087043610126997
  2. 0.149739441826816
  3. 0.197099142521436
  4. 0.240014699961627
  5. 0.278363324822365
  6. 0.315605045837638
  7. 0.349282180622216
  8. 0.378598464546428
  9. 0.405580825770749
  10. 0.429744933496646
  11. 0.451772454422396
  12. 0.472357863116345
  13. 0.491669908919013
  14. 0.510091289940599
  15. 0.527708392613043
  16. 0.544663892810058
  17. 0.561139660129361
  18. 0.577159126860929
  19. 0.592784567417526
  20. 0.60805182027381
  21. 0.622972964543716
  22. 0.63757659436269
  23. 0.651870987707066
  24. 0.665870342243498
  25. 0.679590630547217
  26. 0.693041042292113
A Time Series:
  1. 6.47
  2. 6.32
  3. 6.42
  4. 6.37
  5. 6.37
  6. 5.99
  7. 5.75
  8. 5.31
  9. 5.35
  10. 4.95
  11. 4.72
  12. 4.73
  13. 5.04
  14. 4.89
  15. 5.1
  16. 5.23
  17. 5.29
  18. 5.21
  19. 5.25
  20. 5.31
  21. 5.4
  22. 5.49
  23. 5.62
  24. 5.51
  25. 5.41
  26. 5.47
A Time Series: 26 × 3
ForecastStd. ErrorActual
2008.9426.2073670.087043616.47
2008.9626.2456640.149739446.32
2008.9816.2833790.197099146.42
2009.0006.2843290.240014706.37
2009.0196.2896180.278363326.37
2009.0386.2862490.315605055.99
2009.0586.2679420.349282185.75
2009.0776.2596080.378598465.31
2009.0966.2489740.405580835.35
2009.1156.2402640.429744934.95
2009.1356.2355760.451772454.72
2009.1546.2307800.472357864.73
2009.1736.2291890.491669915.04
2009.1926.2285240.510091294.89
2009.2126.2281470.527708395.10
2009.2316.2290180.544663895.23
2009.2506.2296850.561139665.29
2009.2696.2304540.577159135.21
2009.2886.2312380.592784575.25
2009.3086.2317130.608051825.31
2009.3276.2321720.622972965.40
2009.3466.2324370.637576595.49
2009.3656.2325560.651870995.62
2009.3856.2326390.665870345.51
2009.4046.2326220.679590635.41
2009.4236.2325870.693041045.47
In [495]:
aaa_fc_tb = comb_forecast_res(aaa_fc_res, aaa_ts, eotr, freq)
aaa_fc_res$model['coef'];
sqrt(diag(aaa_fc_res$model[['var.coef']]));
aaa_fc_tb
'The "summary(forecast_obj)" call output length 38321 exceeds the output limit 5000 so that it is suppressed.'
A Time Series:
  1. 6.20736655719426
  2. 6.24566440620478
  3. 6.28337907629132
  4. 6.2843292268527
  5. 6.28961777033179
  6. 6.28624851922467
  7. 6.26794223063653
  8. 6.25960830867983
  9. 6.24897423756542
  10. 6.24026376349615
  11. 6.23557584923931
  12. 6.23078046674665
  13. 6.22918899621492
  14. 6.22852446989816
  15. 6.22814662901021
  16. 6.22901817876502
  17. 6.22968504832111
  18. 6.23045371472518
  19. 6.23123800093802
  20. 6.23171307644446
  21. 6.23217201001091
  22. 6.23243689662713
  23. 6.23255633090181
  24. 6.23263853121758
  25. 6.23262240593548
  26. 6.23258655524976
A Time Series:
  1. 0.087043610126997
  2. 0.149739441826816
  3. 0.197099142521436
  4. 0.240014699961627
  5. 0.278363324822365
  6. 0.315605045837638
  7. 0.349282180622216
  8. 0.378598464546428
  9. 0.405580825770749
  10. 0.429744933496646
  11. 0.451772454422396
  12. 0.472357863116345
  13. 0.491669908919013
  14. 0.510091289940599
  15. 0.527708392613043
  16. 0.544663892810058
  17. 0.561139660129361
  18. 0.577159126860929
  19. 0.592784567417526
  20. 0.60805182027381
  21. 0.622972964543716
  22. 0.63757659436269
  23. 0.651870987707066
  24. 0.665870342243498
  25. 0.679590630547217
  26. 0.693041042292113
A Time Series:
  1. 6.47
  2. 6.32
  3. 6.42
  4. 6.37
  5. 6.37
  6. 5.99
  7. 5.75
  8. 5.31
  9. 5.35
  10. 4.95
  11. 4.72
  12. 4.73
  13. 5.04
  14. 4.89
  15. 5.1
  16. 5.23
  17. 5.29
  18. 5.21
  19. 5.25
  20. 5.31
  21. 5.4
  22. 5.49
  23. 5.62
  24. 5.51
  25. 5.41
  26. 5.47
$coef =
ar1
0.399773430888033
ar2
-0.0871736709916602
ar3
0.106893254240971
ar4
-0.0305155485093501
ar5
0.0836141529103928
ar6
-0.0630714337318061
ar7
-0.0200479940695885
ar8
0.00623385697722459
ar9
-0.0365890449421943
ar1
0.0202318106919676
ar2
0.0218000510381002
ar3
0.0218941498376738
ar4
0.0219618933737215
ar5
0.0218980284932685
ar6
0.0219554357863656
ar7
0.0218817280768451
ar8
0.021812604154254
ar9
0.0202572433182039
A Time Series: 26 × 3
ForecastStd. ErrorActual
2008.9426.2073670.087043616.47
2008.9626.2456640.149739446.32
2008.9816.2833790.197099146.42
2009.0006.2843290.240014706.37
2009.0196.2896180.278363326.37
2009.0386.2862490.315605055.99
2009.0586.2679420.349282185.75
2009.0776.2596080.378598465.31
2009.0966.2489740.405580835.35
2009.1156.2402640.429744934.95
2009.1356.2355760.451772454.72
2009.1546.2307800.472357864.73
2009.1736.2291890.491669915.04
2009.1926.2285240.510091294.89
2009.2126.2281470.527708395.10
2009.2316.2290180.544663895.23
2009.2506.2296850.561139665.29
2009.2696.2304540.577159135.21
2009.2886.2312380.592784575.25
2009.3086.2317130.608051825.31
2009.3276.2321720.622972965.40
2009.3466.2324370.637576595.49
2009.3656.2325560.651870995.62
2009.3856.2326390.665870345.51
2009.4046.2326220.679590635.41
2009.4236.2325870.693041045.47
In [402]:
npts = 26
eotr = length(aaa_ts)-npts
h = npts
freq = 52
order = c(2,1,3)
fixed = NULL
seasonal = NULL
aaa_fc_res_1 = plot_arima_forecast_fig(
    da_ts=aaa_ts, eotr=eotr, h=h, npts=npts, frequency=freq, 
    order=order, seasonal=seasonal, fixed=fixed, method='ML', 
    include.mean=TRUE, transform.pars=TRUE,
    main="Forecasts from ARIMA(2,1,3) for Aaa weekly bond yields", 
    xlab="Year", ylab="Aaa weekly bond yields", ylim=c(4, 8)
)
[1] 2440
2008.423 ; 2009.442
No description has been provided for this image
No description has been provided for this image
In [285]:
# aaa_fc_tb_1 = comb_forecast_res(aaa_fc_res_1, aaa_ts, eotr, freq)
# aaa_fc_tb_1
Forecast method: ARIMA(2,1,3)

Model Information:

Call:
arima(x = tr_da_ts, order = order, seasonal = seasonal, include.mean = include.mean, 
    fixed = fixed, method = method)

Coefficients:
         ar1     ar2     ma1      ma2      ma3
      0.0928  0.3274  0.3084  -0.2907  -0.0277
s.e.  0.1809  0.1336  0.1826   0.1030   0.0537

sigma^2 estimated as 0.007631:  log likelihood = 2485.79,  aic = -4961.58

Error measures:
                       ME       RMSE        MAE         MPE      MAPE
Training set 0.0004452245 0.08733975 0.05630836 0.006843011 0.6904256
                   MASE         ACF1
Training set 0.07407404 2.510243e-05

Forecasts:
         Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
2008.942       6.203303 6.091350 6.315257 6.032086 6.374521
2008.962       6.222434 6.029719 6.415148 5.927702 6.517165
2008.981       6.247466 5.993676 6.501256 5.859328 6.635605
2009.000       6.256052 5.946342 6.565763 5.782391 6.729714
2009.019       6.265046 5.906135 6.623956 5.716140 6.813951
2009.038       6.268691 5.864548 6.672835 5.650608 6.886775
2009.058       6.271974 5.826473 6.717475 5.590639 6.953309
2009.077       6.273473 5.789517 6.757428 5.533327 7.013618
2009.096       6.274687 5.754862 6.794512 5.479683 7.069690
2009.115       6.275290 5.721714 6.828866 5.428668 7.121912
2009.135       6.275743 5.690267 6.861220 5.380334 7.171153
2009.154       6.275983 5.660188 6.891778 5.334206 7.217760
2009.173       6.276154 5.631432 6.920875 5.290137 7.262170
2009.192       6.276248 5.603819 6.948677 5.247857 7.304639
2009.212       6.276313 5.577262 6.975363 5.207207 7.345418
2009.231       6.276350 5.551646 7.001053 5.168012 7.384687
2009.250       6.276374 5.526891 7.025857 5.130139 7.422609
2009.269       6.276388 5.502916 7.049861 5.093465 7.459312
2009.288       6.276398 5.479657 7.073139 5.057888 7.494908
2009.308       6.276403 5.457053 7.095754 5.023315 7.529491
2009.327       6.276407 5.435054 7.117760 4.989669 7.563145
2009.346       6.276409 5.413614 7.139204 4.956878 7.595940
2009.365       6.276411 5.392694 7.160127 4.924882 7.627939
2009.385       6.276411 5.372256 7.180567 4.893625 7.659197
2009.404       6.276412 5.352270 7.200553 4.863059 7.689765
2009.423       6.276412 5.332707 7.220117 4.833140 7.719684
A Time Series:
  1. 6.20330330269006
  2. 6.22243353443424
  3. 6.24746607245773
  4. 6.25605234307303
  5. 6.26504558322429
  6. 6.26869137771544
  7. 6.27197435722184
  8. 6.27347270037949
  9. 6.27468668481252
  10. 6.27528992475406
  11. 6.2757433959077
  12. 6.27598298982167
  13. 6.276153702089
  14. 6.27624799191435
  15. 6.27631263740499
  16. 6.27634950891711
  17. 6.27637409708012
  18. 6.27638845136805
  19. 6.27639783419081
  20. 6.27640340481769
  21. 6.27640699392653
  22. 6.27640915093988
  23. 6.27641052626771
  24. 6.27641136015153
  25. 6.27641188784976
  26. 6.27641220985211
A Time Series:
  1. 0.0873575969045483
  2. 0.150376034883939
  3. 0.198033479167829
  4. 0.241668421990196
  5. 0.280059077131007
  6. 0.315354557154003
  7. 0.347626222826601
  8. 0.377632255950079
  9. 0.405621476362031
  10. 0.431957844624765
  11. 0.45684997407101
  12. 0.480507067616555
  13. 0.503078925929186
  14. 0.524698835464767
  15. 0.545471993235578
  16. 0.565488940301752
  17. 0.584824634122161
  18. 0.603543457479824
  19. 0.621700203437587
  20. 0.63934233836289
  21. 0.656511023822344
  22. 0.673242320749988
  23. 0.68956795117004
  24. 0.705516016121785
  25. 0.721111524730885
  26. 0.736376854489315
A Time Series:
  1. 6.47
  2. 6.32
  3. 6.42
  4. 6.37
  5. 6.37
  6. 5.99
  7. 5.75
  8. 5.31
  9. 5.35
  10. 4.95
  11. 4.72
  12. 4.73
  13. 5.04
  14. 4.89
  15. 5.1
  16. 5.23
  17. 5.29
  18. 5.21
  19. 5.25
  20. 5.31
  21. 5.4
  22. 5.49
  23. 5.62
  24. 5.51
  25. 5.41
  26. 5.47
A Time Series: 26 × 3
ForecastStd. ErrorActual
2008.9426.2033030.08735766.47
2008.9626.2224340.15037606.32
2008.9816.2474660.19803356.42
2009.0006.2560520.24166846.37
2009.0196.2650460.28005916.37
2009.0386.2686910.31535465.99
2009.0586.2719740.34762625.75
2009.0776.2734730.37763235.31
2009.0966.2746870.40562155.35
2009.1156.2752900.43195784.95
2009.1356.2757430.45685004.72
2009.1546.2759830.48050714.73
2009.1736.2761540.50307895.04
2009.1926.2762480.52469884.89
2009.2126.2763130.54547205.10
2009.2316.2763500.56548895.23
2009.2506.2763740.58482465.29
2009.2696.2763880.60354355.21
2009.2886.2763980.62170025.25
2009.3086.2764030.63934235.31
2009.3276.2764070.65651105.40
2009.3466.2764090.67324235.49
2009.3656.2764110.68956805.62
2009.3856.2764110.70551605.51
2009.4046.2764120.72111155.41
2009.4236.2764120.73637695.47
In [498]:
aaa_fc_tb_1 = comb_forecast_res(aaa_fc_res_1, aaa_ts, eotr, freq)
aaa_fc_res_1$model['coef'];
sqrt(diag(aaa_fc_res_1$model[['var.coef']]));
aaa_fc_tb_1
'The "summary(forecast_obj)" call output length 38203 exceeds the output limit 5000 so that it is suppressed.'
A Time Series:
  1. 6.20330330269006
  2. 6.22243353443424
  3. 6.24746607245773
  4. 6.25605234307303
  5. 6.26504558322429
  6. 6.26869137771544
  7. 6.27197435722184
  8. 6.27347270037949
  9. 6.27468668481252
  10. 6.27528992475406
  11. 6.2757433959077
  12. 6.27598298982167
  13. 6.276153702089
  14. 6.27624799191435
  15. 6.27631263740499
  16. 6.27634950891711
  17. 6.27637409708012
  18. 6.27638845136805
  19. 6.27639783419081
  20. 6.27640340481769
  21. 6.27640699392653
  22. 6.27640915093988
  23. 6.27641052626771
  24. 6.27641136015153
  25. 6.27641188784976
  26. 6.27641220985211
A Time Series:
  1. 0.0873575969045483
  2. 0.150376034883939
  3. 0.198033479167829
  4. 0.241668421990196
  5. 0.280059077131007
  6. 0.315354557154003
  7. 0.347626222826601
  8. 0.377632255950079
  9. 0.405621476362031
  10. 0.431957844624765
  11. 0.45684997407101
  12. 0.480507067616555
  13. 0.503078925929186
  14. 0.524698835464767
  15. 0.545471993235578
  16. 0.565488940301752
  17. 0.584824634122161
  18. 0.603543457479824
  19. 0.621700203437587
  20. 0.63934233836289
  21. 0.656511023822344
  22. 0.673242320749988
  23. 0.68956795117004
  24. 0.705516016121785
  25. 0.721111524730885
  26. 0.736376854489315
A Time Series:
  1. 6.47
  2. 6.32
  3. 6.42
  4. 6.37
  5. 6.37
  6. 5.99
  7. 5.75
  8. 5.31
  9. 5.35
  10. 4.95
  11. 4.72
  12. 4.73
  13. 5.04
  14. 4.89
  15. 5.1
  16. 5.23
  17. 5.29
  18. 5.21
  19. 5.25
  20. 5.31
  21. 5.4
  22. 5.49
  23. 5.62
  24. 5.51
  25. 5.41
  26. 5.47
$coef =
ar1
0.0927683901340421
ar2
0.327442053272319
ma1
0.308361671172512
ma2
-0.290724262132678
ma3
-0.0276577320913705
ar1
0.180903706034155
ar2
0.133568778897037
ma1
0.182603007880418
ma2
0.102957951718436
ma3
0.0537454745961446
A Time Series: 26 × 3
ForecastStd. ErrorActual
2008.9426.2033030.08735766.47
2008.9626.2224340.15037606.32
2008.9816.2474660.19803356.42
2009.0006.2560520.24166846.37
2009.0196.2650460.28005916.37
2009.0386.2686910.31535465.99
2009.0586.2719740.34762625.75
2009.0776.2734730.37763235.31
2009.0966.2746870.40562155.35
2009.1156.2752900.43195784.95
2009.1356.2757430.45685004.72
2009.1546.2759830.48050714.73
2009.1736.2761540.50307895.04
2009.1926.2762480.52469884.89
2009.2126.2763130.54547205.10
2009.2316.2763500.56548895.23
2009.2506.2763740.58482465.29
2009.2696.2763880.60354355.21
2009.2886.2763980.62170025.25
2009.3086.2764030.63934235.31
2009.3276.2764070.65651105.40
2009.3466.2764090.67324235.49
2009.3656.2764110.68956805.62
2009.3856.2764110.70551605.51
2009.4046.2764120.72111155.41
2009.4236.2764120.73637695.47
In [373]:
help(arima)
In [499]:
npts = 26
eotr = length(aaa_ts)-npts
h = npts
freq = 52
order = c(2,1,3)
fixed = NULL
seasonal = list(order = c(0, 0, 1), period = 13)
aaa_fc_res_2 = plot_arima_forecast_fig(
    da_ts=aaa_ts, eotr=eotr, h=h, npts=npts, frequency=freq, 
    order=order, seasonal=seasonal, fixed=fixed, method='ML', 
    include.mean=TRUE, transform.pars=TRUE,
    main=NULL, #"Forecasts from ARIMA(2,1,3)(0,0,1)[13] for Aaa weekly bond yields", 
    xlab="Year", ylab="Aaa weekly bond yields", ylim=c(4, 8)
)
[1] 2440
2008.423 ; 2009.442
No description has been provided for this image
No description has been provided for this image
In [500]:
aaa_fc_tb_2 = comb_forecast_res(aaa_fc_res_2, aaa_ts, eotr, freq)
aaa_fc_res_2$model['coef'];
sqrt(diag(aaa_fc_res_2$model[['var.coef']]));
aaa_fc_tb_2
'The "summary(forecast_obj)" call output length 38242 exceeds the output limit 5000 so that it is suppressed.'
A Time Series:
  1. 6.20270523807198
  2. 6.22160075483554
  3. 6.24671166825241
  4. 6.25515994226118
  5. 6.2644297021537
  6. 6.26823265008299
  7. 6.27169925188873
  8. 6.27329035845547
  9. 6.27461704413151
  10. 6.27487212475104
  11. 6.27477349254368
  12. 6.27491385635659
  13. 6.27476603456499
  14. 6.27469206958723
  15. 6.27472172058573
  16. 6.27470854445544
  17. 6.27471754054785
  18. 6.27471379297586
  19. 6.27471654229147
  20. 6.27471549157898
  21. 6.27471633933392
  22. 6.27471605086817
  23. 6.2747163151025
  24. 6.27471623841315
  25. 6.2747163218242
  26. 6.27471630248412
A Time Series:
  1. 0.0873572543942245
  2. 0.150365501637263
  3. 0.198029271675707
  4. 0.241635041799836
  5. 0.279977508345983
  6. 0.315255268828286
  7. 0.347500984131012
  8. 0.377498155902554
  9. 0.405476372972627
  10. 0.431808839993522
  11. 0.456696722854083
  12. 0.480352216605866
  13. 0.502922466634521
  14. 0.524490659658865
  15. 0.545196295859297
  16. 0.565147113111483
  17. 0.584415798838566
  18. 0.603070074216829
  19. 0.621164003324734
  20. 0.638745853393913
  21. 0.655856443666612
  22. 0.672531924155322
  23. 0.688803793066164
  24. 0.70470005972048
  25. 0.720245564179086
  26. 0.735462562921467
A Time Series:
  1. 6.47
  2. 6.32
  3. 6.42
  4. 6.37
  5. 6.37
  6. 5.99
  7. 5.75
  8. 5.31
  9. 5.35
  10. 4.95
  11. 4.72
  12. 4.73
  13. 5.04
  14. 4.89
  15. 5.1
  16. 5.23
  17. 5.29
  18. 5.21
  19. 5.25
  20. 5.31
  21. 5.4
  22. 5.49
  23. 5.62
  24. 5.51
  25. 5.41
  26. 5.47
$coef =
ar1
0.0796114939059718
ar2
0.338776578713575
ma1
0.321378720170081
ma2
-0.296557711007429
ma3
-0.0318653214452775
sma1
-0.0020560964865583
ar1
0.175887279366558
ar2
0.13191759626235
ma1
0.177552707747659
ma2
0.103543200725709
ma3
0.0536919184046071
sma1
0.0203995281895091
A Time Series: 26 × 3
ForecastStd. ErrorActual
2008.9426.2027050.087357256.47
2008.9626.2216010.150365506.32
2008.9816.2467120.198029276.42
2009.0006.2551600.241635046.37
2009.0196.2644300.279977516.37
2009.0386.2682330.315255275.99
2009.0586.2716990.347500985.75
2009.0776.2732900.377498165.31
2009.0966.2746170.405476375.35
2009.1156.2748720.431808844.95
2009.1356.2747730.456696724.72
2009.1546.2749140.480352224.73
2009.1736.2747660.502922475.04
2009.1926.2746920.524490664.89
2009.2126.2747220.545196305.10
2009.2316.2747090.565147115.23
2009.2506.2747180.584415805.29
2009.2696.2747140.603070075.21
2009.2886.2747170.621164005.25
2009.3086.2747150.638745855.31
2009.3276.2747160.655856445.40
2009.3466.2747160.672531925.49
2009.3656.2747160.688803795.62
2009.3856.2747160.704700065.51
2009.4046.2747160.720245565.41
2009.4236.2747160.735462565.47
In [ ]:

2-12 (Aaa and Baa Bond Yields)

In [299]:
da1 = read.table("../AFTS_sol/data/w-Aaa.txt", header = FALSE)
da2 = read.table("../AFTS_sol/data/w-Baa.txt", header = FALSE)
da1[1:5,]; da2[1:5,];
A data.frame: 5 × 4
V1V2V3V4
<int><int><int><dbl>
119621 54.43
219621124.42
319621194.42
419621264.41
519622 24.42
A data.frame: 5 × 4
V1V2V3V4
<int><int><int><dbl>
119621 55.11
219621125.09
319621195.08
419621265.08
519622 25.07
In [302]:
freq = 52
start = c(1962, 1)
aaa = da1[,4]
aaa_ts = ts(aaa, frequency = freq, start = start)
bbb = da2[,4]
bbb_ts = ts(bbb, frequency = freq, start = start)
aaac = diff(aaa)
bbbc = diff(bbb)
aaac_ts = ts(aaac, frequency = freq, start = start)
bbbc_ts = ts(bbbc, frequency = freq, start = start)
In [316]:
apply(cbind(aaac, bbbc), 2, basicStats)
$aaac
A data.frame: 16 × 1
X..newX..i
<dbl>
nobs2466.000000
NAs 0.000000
Minimum -0.700000
Maximum 0.550000
1. Quartile -0.040000
3. Quartile 0.040000
Mean 0.000422
Median 0.000000
Sum 1.040000
SE Mean 0.001946
LCL Mean -0.003394
UCL Mean 0.004238
Variance 0.009339
Stdev 0.096638
Skewness -0.654390
Kurtosis 8.061043
$bbbc
A data.frame: 16 × 1
X..newX..i
<dbl>
nobs2466.000000
NAs 0.000000
Minimum -0.680000
Maximum 0.840000
1. Quartile -0.040000
3. Quartile 0.040000
Mean 0.001407
Median 0.000000
Sum 3.470000
SE Mean 0.001714
LCL Mean -0.001954
UCL Mean 0.004768
Variance 0.007244
Stdev 0.085113
Skewness 0.133853
Kurtosis 9.247167

Exploration

In [301]:
par(bg = 'white')
plot(aaa_ts, main = "Weekly Aaa or Bbb Bond Yield", xlab = "Year", ylab = "Yield")
lines(bbb_ts, lty = 2)
legend(
    "topleft",
    legend = c("Aaa", "Bbb"),
    lty = c(1, 2)
)
No description has been provided for this image
In [304]:
par(bg = 'white')
plot(aaac_ts, main = "Weekly Aaa Bond Yield", xlab = "Year", ylab = "Yield")
plot(bbbc_ts, main = "Weekly Bbb Bond Yield", xlab = "Year", ylab = "Yield")
No description has been provided for this image
No description has been provided for this image
In [305]:
par(bg = "white")
plot(x = aaa, y = bbb, main = "Aaa vs Bbb", xlab = "Aaa Yield", ylab = "Bbb Yield")
plot(x = aaac, y = bbbc, main = "Aaa Change vs Bbb Change", xlab = "diff(Aaa Yield)", ylab = "diff(Bbb Yield)")
No description has been provided for this image
No description has been provided for this image

Fitting

  • Strong serial correlation in ACF of residuals, indicating that there is a unit-root process.
  • Also conducted ADF unit-root tests. The results show that null-hypothesis cannot be rejected.
    • Null-hypothesis is that the time series has unit-root.
  • Box-Ljung test shows that numm-hypothesis can be rejected.
    • Null-hypothesis is that the time series at certain lag has no autocorrelation.
  • After taking one diff, the autocorrelation is largely gone. But Box-Ljung test is still significant. We can model the residuals as a time series.

Fitting the bond yields

In [306]:
bond_m1 = lm(aaa~bbb)
summary(bond_m1)
Call:
lm(formula = aaa ~ bbb)

Residuals:
     Min       1Q   Median       3Q      Max 
-2.46094 -0.14265  0.06717  0.20420  0.68515 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 0.030569   0.023025   1.328    0.184    
bbb         0.881591   0.002488 354.350   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.3357 on 2465 degrees of freedom
Multiple R-squared:  0.9807,	Adjusted R-squared:  0.9807 
F-statistic: 1.256e+05 on 1 and 2465 DF,  p-value: < 2.2e-16
In [308]:
par(bg = 'white')
resi_ts = ts(bond_m1$residuals, frequency = freq, start = start)
plot(resi_ts, type = 'l', main = "Simple Linear Model", xlab = "Year")
lag.max = 36
par(mfrow = c(2,1))
pacf(bond_m1$residuals, lag.max = lag.max, main = "Residuals PACF Plot")
acf(bond_m1$residuals, lag.max = lag.max, main = "Residuals ACF Plot")
No description has been provided for this image
No description has been provided for this image
In [309]:
adf_test_res_a = adfTest(aaa, lags = 12, type = c("ct"))
adf_test_res_b = adfTest(bbb, lags = 12, type = c("ct")) # 2.7.3. Trend-Stationary Time-Series
adf_test_res_a; adf_test_res_b
Title:
 Augmented Dickey-Fuller Test

Test Results:
  PARAMETER:
    Lag Order: 12
  STATISTIC:
    Dickey-Fuller: -1.592
  P VALUE:
    0.7511 

Description:
 Thu Feb 29 12:01:44 2024 by user: 
Title:
 Augmented Dickey-Fuller Test

Test Results:
  PARAMETER:
    Lag Order: 12
  STATISTIC:
    Dickey-Fuller: -1.6934
  P VALUE:
    0.7081 

Description:
 Thu Feb 29 12:01:44 2024 by user: 
In [310]:
Box.test(bond_m1$residuals, lag = 12, type = 'Ljung')
	Box-Ljung test

data:  bond_m1$residuals
X-squared = 21007, df = 12, p-value < 2.2e-16

Fitting the bond yield changes

In [311]:
bond_m2 = lm(aaac~-1+bbbc) # No intercept
summary(bond_m2)
Call:
lm(formula = aaac ~ -1 + bbbc)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.44475 -0.01838  0.00000  0.01946  0.36192 

Coefficients:
     Estimate Std. Error t value Pr(>|t|)    
bbbc  0.94613    0.01264   74.87   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.05341 on 2465 degrees of freedom
Multiple R-squared:  0.6946,	Adjusted R-squared:  0.6944 
F-statistic:  5605 on 1 and 2465 DF,  p-value: < 2.2e-16
In [312]:
par(bg = 'white')
resi_ts = ts(bond_m2$residuals, frequency = freq, start = start)
plot(resi_ts, type = 'l', main = "Simple Linear Model", xlab = "Year")
lag.max = 36
par(mfrow = c(2,1))
pacf(bond_m2$residuals, lag.max = lag.max, main = "Residuals PACF Plot")
acf(bond_m2$residuals, lag.max = lag.max, main = "Residuals ACF Plot")
No description has been provided for this image
No description has been provided for this image
In [314]:
adf_test_res_ac = adfTest(aaac, lags = 12, type = c("ct"))
adf_test_res_bc = adfTest(bbbc, lags = 12, type = c("ct")) # 2.7.3. Trend-Stationary Time-Series
adf_test_res_ac; adf_test_res_bc
Warning message in adfTest(aaac, lags = 12, type = c("ct")):
“p-value smaller than printed p-value”
Warning message in adfTest(bbbc, lags = 12, type = c("ct")):
“p-value smaller than printed p-value”
Title:
 Augmented Dickey-Fuller Test

Test Results:
  PARAMETER:
    Lag Order: 12
  STATISTIC:
    Dickey-Fuller: -14.495
  P VALUE:
    0.01 

Description:
 Thu Feb 29 12:09:09 2024 by user: 
Title:
 Augmented Dickey-Fuller Test

Test Results:
  PARAMETER:
    Lag Order: 12
  STATISTIC:
    Dickey-Fuller: -13.0501
  P VALUE:
    0.01 

Description:
 Thu Feb 29 12:09:09 2024 by user: 
In [313]:
Box.test(bond_m2$residuals, lag = 12, type = 'Ljung')
	Box-Ljung test

data:  bond_m2$residuals
X-squared = 189.69, df = 12, p-value < 2.2e-16

Fitting a time series residuals

In [587]:
bond_m3 = arima(aaac, order = c(0,0,1), xreg = bbbc, include.mean = FALSE)
# rsq = 1 - sum(m3$residuals^2)/sum(c3^2-mean(c3)) # B/c in the model we set `include.mean = F`, so here when evaluating R-squared, we don't include the mean either.
rsq = 1 - sum(bond_m3$residuals^2)/sum(aaac^2)
summary(bond_m3); rsq;
par(bg = 'white')
tsdiag(bond_m3, gof = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = aaac, order = c(0, 0, 1), xreg = bbbc, include.mean = FALSE)

Coefficients:
         ma1    xreg
      0.2335  0.9436
s.e.  0.0185  0.0132

sigma^2 estimated as 0.00269:  log likelihood = 3797.81,  aic = -7591.62

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.711795207528689
No description has been provided for this image
In [588]:
bond_m3 = arima(aaac, order = c(1,0,0), xreg = bbbc, include.mean = FALSE)
# rsq = 1 - sum(m3$residuals^2)/sum(c3^2-mean(c3)) # B/c in the model we set `include.mean = F`, so here when evaluating R-squared, we don't include the mean either.
rsq = 1 - sum(bond_m3$residuals^2)/sum(aaac^2)
summary(bond_m3); rsq;
par(bg = 'white')
tsdiag(bond_m3, gof = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = aaac, order = c(1, 0, 0), xreg = bbbc, include.mean = FALSE)

Coefficients:
         ar1    xreg
      0.2394  0.9472
s.e.  0.0195  0.0133

sigma^2 estimated as 0.002688:  log likelihood = 3798.99,  aic = -7593.98

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.71207055950736
No description has been provided for this image
In [589]:
bond_m3 = arima(aaac, order = c(1,0,1), xreg = bbbc, include.mean = FALSE)
# rsq = 1 - sum(m3$residuals^2)/sum(c3^2-mean(c3)) # B/c in the model we set `include.mean = F`, so here when evaluating R-squared, we don't include the mean either.
rsq = 1 - sum(bond_m3$residuals^2)/sum(aaac^2)
summary(bond_m3); rsq;
par(bg = 'white')
tsdiag(bond_m3, gof = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = aaac, order = c(1, 0, 1), xreg = bbbc, include.mean = FALSE)

Coefficients:
         ar1     ma1    xreg
      0.1476  0.0980  0.9455
s.e.  0.0710  0.0706  0.0133

sigma^2 estimated as 0.002686:  log likelihood = 3799.9,  aic = -7593.81

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.71228449137804
No description has been provided for this image
In [ ]:

In [324]:
cat("order =", ar(bond_m2$residuals, method = 'mle')$order, "\n")
order = 12 
In [579]:
bond_m3 = arima(aaac, order = c(12,0,0), xreg = bbbc, include.mean = FALSE)
# rsq = 1 - sum(m3$residuals^2)/sum(c3^2-mean(c3)) # B/c in the model we set `include.mean = F`, so here when evaluating R-squared, we don't include the mean either.
rsq = 1 - sum(bond_m3$residuals^2)/sum(aaac^2)
summary(bond_m3); rsq;
par(bg = 'white')
tsdiag(bond_m3, gof = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = aaac, order = c(12, 0, 0), xreg = bbbc, include.mean = FALSE)

Coefficients:
         ar1      ar2      ar3     ar4     ar5      ar6      ar7      ar8
      0.2430  -0.0208  -0.0432  0.0632  0.0233  -0.0375  -0.0388  -0.0215
s.e.  0.0201   0.0208   0.0207  0.0208  0.0208   0.0208   0.0209   0.0208
          ar9     ar10     ar11    ar12    xreg
      -0.0129  -0.0235  -0.0451  0.0294  0.9409
s.e.   0.0208   0.0207   0.0207  0.0202  0.0133

sigma^2 estimated as 0.002646:  log likelihood = 3818.08,  aic = -7610.16

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.716505708872977
No description has been provided for this image
In [580]:
bond_m3 = arima(aaac, order = c(2,0,0), xreg = bbbc, include.mean = FALSE)
# rsq = 1 - sum(m3$residuals^2)/sum(c3^2-mean(c3)) # B/c in the model we set `include.mean = F`, so here when evaluating R-squared, we don't include the mean either.
rsq = 1 - sum(bond_m3$residuals^2)/sum(aaac^2)
summary(bond_m3); rsq;
par(bg = 'white')
tsdiag(bond_m3, gof = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = aaac, order = c(2, 0, 0), xreg = bbbc, include.mean = FALSE)

Coefficients:
         ar1      ar2    xreg
      0.2468  -0.0310  0.9453
s.e.  0.0201   0.0202  0.0133

sigma^2 estimated as 0.002685:  log likelihood = 3800.16,  aic = -7594.32

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.712344885253492
No description has been provided for this image
In [581]:
bond_m3 = arima(aaac, order = c(3,0,0), xreg = bbbc, include.mean = FALSE)
# rsq = 1 - sum(m3$residuals^2)/sum(c3^2-mean(c3)) # B/c in the model we set `include.mean = F`, so here when evaluating R-squared, we don't include the mean either.
rsq = 1 - sum(bond_m3$residuals^2)/sum(aaac^2)
summary(bond_m3); rsq;
par(bg = 'white')
tsdiag(bond_m3, gof = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = aaac, order = c(3, 0, 0), xreg = bbbc, include.mean = FALSE)

Coefficients:
         ar1      ar2      ar3    xreg
      0.2459  -0.0245  -0.0257  0.9463
s.e.  0.0201   0.0208   0.0202  0.0133

sigma^2 estimated as 0.002684:  log likelihood = 3800.97,  aic = -7593.94

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.712533599591422
No description has been provided for this image
In [582]:
bond_m3 = arima(aaac, order = c(4,0,0), xreg = bbbc, include.mean = FALSE)
# rsq = 1 - sum(m3$residuals^2)/sum(c3^2-mean(c3)) # B/c in the model we set `include.mean = F`, so here when evaluating R-squared, we don't include the mean either.
rsq = 1 - sum(bond_m3$residuals^2)/sum(aaac^2)
summary(bond_m3); rsq;
par(bg = 'white')
tsdiag(bond_m3, gof = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = aaac, order = c(4, 0, 0), xreg = bbbc, include.mean = FALSE)

Coefficients:
         ar1      ar2      ar3     ar4    xreg
      0.2477  -0.0226  -0.0430  0.0698  0.9475
s.e.  0.0201   0.0208   0.0207  0.0201  0.0132

sigma^2 estimated as 0.002671:  log likelihood = 3806.98,  aic = -7603.96

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.713933383036969
No description has been provided for this image
In [583]:
bond_m3 = arima(aaac, order = c(5,0,0), xreg = bbbc, include.mean = FALSE)
# rsq = 1 - sum(m3$residuals^2)/sum(c3^2-mean(c3)) # B/c in the model we set `include.mean = F`, so here when evaluating R-squared, we don't include the mean either.
rsq = 1 - sum(bond_m3$residuals^2)/sum(aaac^2)
summary(bond_m3); rsq;
par(bg = 'white')
tsdiag(bond_m3, gof = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = aaac, order = c(5, 0, 0), xreg = bbbc, include.mean = FALSE)

Coefficients:
         ar1      ar2      ar3     ar4     ar5    xreg
      0.2469  -0.0222  -0.0427  0.0672  0.0107  0.9475
s.e.  0.0201   0.0208   0.0207  0.0207  0.0202  0.0133

sigma^2 estimated as 0.00267:  log likelihood = 3807.12,  aic = -7602.24

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.713965869272248
No description has been provided for this image
In [584]:
bond_m3 = arima(aaac, order = c(8,0,0), xreg = bbbc, include.mean = FALSE)
# rsq = 1 - sum(m3$residuals^2)/sum(c3^2-mean(c3)) # B/c in the model we set `include.mean = F`, so here when evaluating R-squared, we don't include the mean either.
rsq = 1 - sum(bond_m3$residuals^2)/sum(aaac^2)
summary(bond_m3); rsq;
par(bg = 'white')
tsdiag(bond_m3, gof = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = aaac, order = c(8, 0, 0), xreg = bbbc, include.mean = FALSE)

Coefficients:
         ar1      ar2      ar3     ar4     ar5      ar6      ar7      ar8
      0.2443  -0.0194  -0.0410  0.0652  0.0215  -0.0408  -0.0385  -0.0226
s.e.  0.0201   0.0208   0.0208  0.0207  0.0207   0.0207   0.0208   0.0202
        xreg
      0.9424
s.e.  0.0133

sigma^2 estimated as 0.002657:  log likelihood = 3813.3,  aic = -7608.6

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.715400138593424
No description has been provided for this image
In [585]:
bond_m3 = arima(aaac, order = c(2,0,2), xreg = bbbc, include.mean = FALSE)
# rsq = 1 - sum(m3$residuals^2)/sum(c3^2-mean(c3)) # B/c in the model we set `include.mean = F`, so here when evaluating R-squared, we don't include the mean either.
rsq = 1 - sum(bond_m3$residuals^2)/sum(aaac^2)
summary(bond_m3); rsq;
par(bg = 'white')
tsdiag(bond_m3, gof = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = aaac, order = c(2, 0, 2), xreg = bbbc, include.mean = FALSE)

Coefficients:
          ar1      ar2     ma1     ma2    xreg
      -0.1971  -0.2378  0.4430  0.3195  0.9469
s.e.   0.2242   0.1148  0.2255  0.0810  0.0132

sigma^2 estimated as 0.002675:  log likelihood = 3805.1,  aic = -7600.21

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.713497442999915
No description has been provided for this image
In [586]:
bond_m3 = arima(
    aaac, order = c(2,0,2), 
    fixed = c(0, NA, NA, NA, NA),
    xreg = bbbc, include.mean = FALSE
)
# rsq = 1 - sum(m3$residuals^2)/sum(c3^2-mean(c3)) # B/c in the model we set `include.mean = F`, so here when evaluating R-squared, we don't include the mean either.
rsq = 1 - sum(bond_m3$residuals^2)/sum(aaac^2)
summary(bond_m3); rsq;
par(bg = 'white')
tsdiag(bond_m3, gof = 36)
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“some AR parameters were fixed: setting transform.pars = FALSE”
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = aaac, order = c(2, 0, 2), xreg = bbbc, include.mean = FALSE, fixed = c(0, 
    NA, NA, NA, NA))

Coefficients:
      ar1      ar2     ma1     ma2    xreg
        0  -0.2829  0.2464  0.3113  0.9464
s.e.    0   0.0982  0.0205  0.0925  0.0131

sigma^2 estimated as 0.002676:  log likelihood = 3804.53,  aic = -7601.07

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.713364728525762
No description has been provided for this image

Forecast

In [332]:
npts = 26
eotr = length(aaac_ts)-npts
h = npts
freq = 52
xreg = as.matrix(bbbc)
aaac_fc_res = plot_auto_arima_forecast_fig(
    da_ts=aaac_ts, eotr=eotr, h=h, npts=npts, frequency=freq,
    xreg=xreg,
    main=NULL, # "Forecasts from ARIMA(2,0,2)\nfor CRSP Equal-Weighted Index",
    xlab="Year", ylab="Aaa Bond Yield (using Baa Bond Yield)", # , ylim=c(5, 11)
    d = 0,
    D = 0,
    max.p = 3,
    max.q = 4,
    max.P = 0,
    max.Q = 0,
    max.order = 5,
    seasonal = FALSE,
    method = "ML",
    allowmean = FALSE,
    stepwise = FALSE,
    parallel = TRUE,
    num.cores = 2
)
[1] 2440
2008.404 ; 2009.423
No description has been provided for this image
In [333]:
aaac_fc_tb = comb_forecast_res(aaac_fc_res, aaac_ts, eotr, freq)
aaac_fc_tb
Forecast method: Regression with ARIMA(2,0,3) errors

Model Information:
Series: tr_da_ts 
Regression with ARIMA(2,0,3) errors 

Coefficients:
          ar1      ar2     ma1     ma2     ma3    xreg
      -0.0073  -0.9447  0.2120  0.9378  0.1590  0.9570
s.e.   0.0154   0.0361  0.0253  0.0396  0.0236  0.0131

sigma^2 = 0.002507:  log likelihood = 3846.97
AIC=-7679.95   AICc=-7679.9   BIC=-7639.35

Error measures:
                        ME       RMSE        MAE MPE MAPE     MASE        ACF1
Training set -0.0004666278 0.05000607 0.03054997 NaN  Inf 0.344656 0.006556348

Forecasts:
         Point Forecast        Lo 80        Hi 80        Lo 95       Hi 95
2008.923    0.768124723  0.703960422  0.832289023  0.669993894  0.86625555
2008.942    0.196099159  0.130605234  0.261593084  0.095934844  0.29626347
2008.962    0.195199605  0.129703479  0.260695730  0.095031925  0.29536728
2008.981   -0.157585923 -0.223118946 -0.092052899 -0.257810033 -0.05736181
2009.000   -0.070542178 -0.136077291 -0.005007065 -0.170769484  0.02968513
2009.019   -0.110605586 -0.176173488 -0.045037684 -0.210883039 -0.01032813
2009.038   -0.034959451 -0.100529333  0.030610431 -0.135239932  0.06532103
2009.058   -0.310281374 -0.375880393 -0.244682355 -0.410606416 -0.20995633
2009.077   -0.060531180 -0.126132072  0.005069712 -0.160859086  0.03979673
2009.096   -0.398127384 -0.463754165 -0.332500603 -0.498494884 -0.29775988
2009.115   -0.198069283 -0.263697832 -0.132440733 -0.298439487 -0.09769908
2009.135   -0.022779233 -0.088430784  0.042872318 -0.123184616  0.07762615
2009.154    0.150404876  0.084751658  0.216058094  0.049996943  0.25081281
2009.173   -0.245372922 -0.311046575 -0.179699268 -0.345812107 -0.14493374
2009.192    0.174812481  0.109137258  0.240487703  0.074370895  0.27525407
2009.212    0.063708105 -0.001985271  0.129401481 -0.036761243  0.16417745
2009.231   -0.040662205 -0.106357056  0.025032645 -0.141133809  0.05980940
2009.250   -0.159576203 -0.225287178 -0.093865228 -0.260072467 -0.05907994
2009.269    0.002226129 -0.063486230  0.067938489 -0.098272252  0.10272451
2009.288    0.111880532  0.046153851  0.177607213  0.011360248  0.21240082
2009.308    0.093622583  0.027894605  0.159350562 -0.006899685  0.19414485
2009.327    0.165512032  0.099771335  0.231252729  0.064970312  0.26605375
2009.346    0.097649441  0.031907530  0.163391353 -0.002894136  0.19819302
2009.365    0.026037185 -0.039716021  0.091790390 -0.074523665  0.12659803
2009.385   -0.059240699 -0.124995040  0.006513642 -0.159803286  0.04132189
2009.404    0.107813680  0.042049311  0.173578049  0.007235757  0.20839160
A Time Series:
  1. 0.768124722892773
  2. 0.196099158934993
  3. 0.195199604711239
  4. -0.157585922811906
  5. -0.0705421780087563
  6. -0.110605585759758
  7. -0.0349594511087951
  8. -0.31028137413802
  9. -0.060531180049755
  10. -0.398127383690043
  11. -0.198069282747511
  12. -0.0227792332303308
  13. 0.15040487594322
  14. -0.245372921535585
  15. 0.17481248068782
  16. 0.0637081051064078
  17. -0.0406622054036674
  18. -0.159576202991684
  19. 0.00222612936507818
  20. 0.111880531657912
  21. 0.093622583315119
  22. 0.165512031566735
  23. 0.0976494413530556
  24. 0.0260371848928738
  25. -0.0592406989075704
  26. 0.107813679982687
A Time Series:
  1. 0.0500676696689963
  2. 0.0511051812392695
  3. 0.0511068983526645
  4. 0.0511356898710673
  5. 0.0511373202138411
  6. 0.0511629059237635
  7. 0.0511644508783508
  8. 0.0511871863791592
  9. 0.0511886477400432
  10. 0.0512088491938431
  11. 0.051210229095681
  12. 0.0512281776296255
  13. 0.0512294784825296
  14. 0.0512454240904818
  15. 0.0512466485221148
  16. 0.0512608135248649
  17. 0.0512619643289661
  18. 0.0512745464447021
  19. 0.0512756265356275
  20. 0.0512868016059121
  21. 0.051287813978761
  22. 0.0512977383897053
  23. 0.0512986860855978
  24. 0.0513074989191092
  25. 0.0513083849951865
  26. 0.0513162099409904
A Time Series:
  1. 0.35
  2. -0.149999999999999
  3. 0.0999999999999996
  4. -0.0499999999999998
  5. 0
  6. -0.38
  7. -0.24
  8. -0.44
  9. 0.04
  10. -0.399999999999999
  11. -0.23
  12. 0.0100000000000007
  13. 0.31
  14. -0.15
  15. 0.21
  16. 0.130000000000001
  17. 0.0599999999999996
  18. -0.0800000000000001
  19. 0.04
  20. 0.0599999999999996
  21. 0.0900000000000007
  22. 0.0899999999999999
  23. 0.13
  24. -0.11
  25. -0.0999999999999996
  26. 0.0599999999999996
A Time Series: 26 × 3
ForecastStd. ErrorActual
2008.923 0.7681247230.05006767 0.35
2008.942 0.1960991590.05110518-0.15
2008.962 0.1951996050.05110690 0.10
2008.981-0.1575859230.05113569-0.05
2009.000-0.0705421780.05113732 0.00
2009.019-0.1106055860.05116291-0.38
2009.038-0.0349594510.05116445-0.24
2009.058-0.3102813740.05118719-0.44
2009.077-0.0605311800.05118865 0.04
2009.096-0.3981273840.05120885-0.40
2009.115-0.1980692830.05121023-0.23
2009.135-0.0227792330.05122818 0.01
2009.154 0.1504048760.05122948 0.31
2009.173-0.2453729220.05124542-0.15
2009.192 0.1748124810.05124665 0.21
2009.212 0.0637081050.05126081 0.13
2009.231-0.0406622050.05126196 0.06
2009.250-0.1595762030.05127455-0.08
2009.269 0.0022261290.05127563 0.04
2009.288 0.1118805320.05128680 0.06
2009.308 0.0936225830.05128781 0.09
2009.327 0.1655120320.05129774 0.09
2009.346 0.0976494410.05129869 0.13
2009.365 0.0260371850.05130750-0.11
2009.385-0.0592406990.05130838-0.10
2009.404 0.1078136800.05131621 0.06
In [342]:
par(bg = 'white')
tsdiag(aaac_fc_res$model, gof.lag = 36)
No description has been provided for this image
In [ ]:

2-13 (CRSP Equal-Weighted Index)

  • The data are monthly log-return.
  • Interesting observations.
    • The ew_lrtn has medium momentum at lag-1, and no autocorrelation for longer lags.
In [453]:
library(lmtest)
In [454]:
da = read.table("../AFTS_sol/data/m-ew6299.txt", header = FALSE)
ew_lrtn = da[,1]
ew_lrtn_ts = ts(ew_lrtn, frequency = 12, start = c(1962, 1))
ew_prr = cumsum(ew_lrtn)
ew_prr_ts = ts(ew_prr, frequency = 12, start = c(1962, 1))
dim(da); da[1:5, ]
  1. 456
  2. 1
  1. -0.792
  2. 1.532
  3. -0.596
  4. -7.049
  5. -10.319
In [455]:
start(ew_prr_ts)
  1. 1962
  2. 1
In [659]:
t_offset = c(10, 0)
plot_time_fig(ew_prr_ts, main = "CRSP Equal-Weighted Index", xlab = "Year", ylab = "lg(pr_ratio)")
plot_time_fig(
    window(ew_prr_ts, start = start(ew_prr_ts), end = start(ew_prr_ts) + t_offset), 
    main = "CRSP Equal-Weighted Index", xlab = "Year", ylab = "lg(pr_ratio)"
)
plot_time_fig(
    window(ew_prr_ts, start = end(ew_prr_ts) - t_offset, end = end(ew_prr_ts)), 
    main = "CRSP Equal-Weighted Index", xlab = "Year", ylab = "lg(pr_ratio)"
)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [660]:
t_offset = c(10, 0)
plot_time_fig(ew_lrtn_ts, main = "CRSP Equal-Weighted Index", xlab = "Year", ylab = "lg(rtn)")
plot_time_fig(
    window(ew_lrtn_ts, start = start(ew_lrtn_ts), end = start(ew_lrtn_ts) + t_offset),
    main = "CRSP Equal-Weighted Index", xlab = "Year", ylab = "lg(pr_ratio)"
)
plot_time_fig(
    window(ew_lrtn_ts, start = end(ew_lrtn_ts) - t_offset, end = end(ew_lrtn_ts)), 
    main = "CRSP Equal-Weighted Index", xlab = "Year", ylab = "lg(pr_ratio)"
)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [573]:
plot_pacf_acf(ew_lrtn, freq = 12, lag.max = 36)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image

2-13 (a)

  • The AR(1, 0, 0) looks adequate.
In [577]:
cat("order =", ar(ew_lrtn, method = 'mle')$order, "\n")
order = 1 
In [639]:
ew_lrtn_m1 = arima(ew_lrtn_ts, order = c(1, 0, 0), method = 'ML')
rsq = 1 - sum(ew_lrtn_m1$residuals^2)/sum(ew_lrtn^2)
summary(ew_lrtn_m1); rsq;
par(bg = 'white')
tsdiag1(ew_lrtn_m1, gof.lag = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = ew_lrtn_ts, order = c(1, 0, 0), method = "ML")

Coefficients:
         ar1  intercept
      0.2267     1.0625
s.e.  0.0456     0.3297

sigma^2 estimated as 29.68:  log likelihood = -1420.11,  aic = 2844.22

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.0842673685244923
No description has been provided for this image
In [646]:
ew_lrtn_m2 = arima(ew_lrtn_ts, order = c(2, 0, 0), method = 'ML')
rsq = 1 - sum(ew_lrtn_m2$residuals^2)/sum(ew_lrtn^2)
summary(ew_lrtn_m2); rsq;
par(bg = 'white')
tsdiag1(ew_lrtn_m2, gof = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = ew_lrtn_ts, order = c(2, 0, 0), method = "ML")

Coefficients:
         ar1      ar2  intercept
      0.2410  -0.0640     1.0602
s.e.  0.0467   0.0468     0.3093

sigma^2 estimated as 29.56:  log likelihood = -1419.18,  aic = 2844.35

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.0880283772678206
No description has been provided for this image

2-13 (b)

  • The MA(0, 0, 1) looks adequate.
In [737]:
ew_lrtn_m3 = arima(ew_lrtn_ts, order = c(0, 0, 1), method = 'ML')
rsq = 1 - sum(ew_lrtn_m3$residuals^2)/sum(ew_lrtn^2)
summary(ew_lrtn_m3); rsq;
par(bg = 'white')
tsdiag1(ew_lrtn_m3, gof = 36)
# likelihood-ratio test
llike_rtest_pv <- pchisq(
    q = 2*(ew_lrtn_m3$loglik-ew_lrtn_m1$loglik),
    df = length(ew_lrtn_m3$coef)-length(ew_lrtn_m1$coef),
    lower.tail = FALSE
)
# cat("Likelihood-Ratio test p-value:", llike_rtest_pv, "")
lrt_res <- lrtest(ew_lrtn_m1, ew_lrtn_m3)
sprintf(
    "Likelihood-Ratio test p-value: %f (lrtest: %d; %f; %f)", 
    llike_rtest_pv,
    lrt_res$Df[2],
    lrt_res$Chisq[2],
    lrt_res$`Pr(>Chisq)`[2]
)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = ew_lrtn_ts, order = c(0, 0, 1), method = "ML")

Coefficients:
         ma1  intercept
      0.2385     1.0603
s.e.  0.0449     0.3153

sigma^2 estimated as 29.59:  log likelihood = -1419.37,  aic = 2842.73

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.0872669416270972
'Likelihood-Ratio test p-value: 0.000000 (lrtest: 0; 1.490312; 0.000000)'
No description has been provided for this image
In [648]:
ew_lrtn_m4 = arima(ew_lrtn_ts, order = c(0, 0, 2), method = 'ML')
rsq = 1 - sum(ew_lrtn_m4$residuals^2)/sum(ew_lrtn^2)
summary(ew_lrtn_m4); rsq;
par(bg = 'white')
tsdiag1(ew_lrtn_m4, gof = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = ew_lrtn_ts, order = c(0, 0, 2), method = "ML")

Coefficients:
         ma1     ma2  intercept
      0.2403  0.0064     1.0602
s.e.  0.0473  0.0484     0.3174

sigma^2 estimated as 29.58:  log likelihood = -1419.36,  aic = 2844.71

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.0873020956499592
No description has been provided for this image
In [650]:
ew_lrtn_m5 = arima(ew_lrtn_ts, order = c(1, 0, 1), method = 'ML')
rsq = 1 - sum(ew_lrtn_m5$residuals^2)/sum(ew_lrtn^2)
summary(ew_lrtn_m5); rsq;
par(bg = 'white')
tsdiag1(ew_lrtn_m5, gof = 36)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = ew_lrtn_ts, order = c(1, 0, 1), method = "ML")

Coefficients:
         ar1     ma1  intercept
      0.0193  0.2205     1.0604
s.e.  0.1785  0.1729     0.3169

sigma^2 estimated as 29.58:  log likelihood = -1419.36,  aic = 2844.72

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.0872942546222992
No description has been provided for this image
In [736]:
ew_lrtn_m6 = arima(ew_lrtn_ts, order = c(1, 0, 0), seasonal = c(1, 0, 0), method = 'ML')
rsq = 1 - sum(ew_lrtn_m6$residuals^2)/sum(ew_lrtn^2)
summary(ew_lrtn_m6); rsq;
par(bg = 'white')
tsdiag1(ew_lrtn_m6, gof = 36)
# likelihood-ratio test
llike_rtest_pv <- pchisq(
    q = 2*(ew_lrtn_m6$loglik-ew_lrtn_m1$loglik),
    df = length(ew_lrtn_m6$coef)-length(ew_lrtn_m1$coef),
    lower.tail = FALSE
)
# cat("Likelihood-Ratio test p-value:", llike_rtest_pv, "")
lrt_res <- lrtest(ew_lrtn_m1, ew_lrtn_m6)
sprintf(
    "Likelihood-Ratio test p-value: %f (lrtest: %d; %f; %f)", 
    llike_rtest_pv,
    lrt_res$Df[2],
    lrt_res$Chisq[2],
    lrt_res$`Pr(>Chisq)`[2]
)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = ew_lrtn_ts, order = c(1, 0, 0), seasonal = c(1, 0, 0), method = "ML")

Coefficients:
         ar1    sar1  intercept
      0.2287  0.1119     1.0592
s.e.  0.0456  0.0472     0.3687

sigma^2 estimated as 29.31:  log likelihood = -1417.32,  aic = 2840.65

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.09569205340984
'Likelihood-Ratio test p-value: 0.018244 (lrtest: 1; 5.572591; 0.018244)'
No description has been provided for this image
In [735]:
ew_lrtn_m7 = arima(ew_lrtn_ts, order = c(0, 0, 1), seasonal = c(0, 0, 1), method = 'ML')
rsq = 1 - sum(ew_lrtn_m7$residuals^2)/sum(ew_lrtn^2)
summary(ew_lrtn_m7); rsq;
par(bg = 'white')
tsdiag1(ew_lrtn_m7, gof = 36)
# likelihood-ratio test
llike_rtest_pv <- pchisq(
    q = 2*(ew_lrtn_m7$loglik-ew_lrtn_m3$loglik),
    df = length(ew_lrtn_m7$coef)-length(ew_lrtn_m3$coef),
    lower.tail = FALSE
)
# cat("Likelihood-Ratio test p-value:", llike_rtest_pv, "")
lrt_res <- lrtest(ew_lrtn_m3, ew_lrtn_m7)
sprintf(
    "Likelihood-Ratio test p-value: %f (lrtest: %d; %f; %f)", 
    llike_rtest_pv,
    lrt_res$Df[2],
    lrt_res$Chisq[2],
    lrt_res$`Pr(>Chisq)`[2]
)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = ew_lrtn_ts, order = c(0, 0, 1), seasonal = c(0, 0, 1), method = "ML")

Coefficients:
         ma1    sma1  intercept
      0.2360  0.1034     1.0587
s.e.  0.0445  0.0463     0.3445

sigma^2 estimated as 29.26:  log likelihood = -1416.89,  aic = 2839.78

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.0973783737394514
'Likelihood-Ratio test p-value: 0.026059 (lrtest: 1; 4.952098; 0.026059)'
No description has been provided for this image
In [744]:
ew_lrtn_m8 = arima(ew_lrtn_ts, order = c(1, 0, 0), seasonal = c(1, 0, 1), method = 'ML')
rsq = 1 - sum(ew_lrtn_m8$residuals^2)/sum(ew_lrtn^2)
summary(ew_lrtn_m8); rsq;
par(bg = 'white')
tsdiag1(ew_lrtn_m8, gof = 36)
# likelihood-ratio test
llike_rtest_pv <- pchisq(
    q = 2*(ew_lrtn_m8$loglik-ew_lrtn_m7$loglik),
    df = length(ew_lrtn_m8$coef)-length(ew_lrtn_m7$coef),
    lower.tail = FALSE
)
# cat("Likelihood-Ratio test p-value:", llike_rtest_pv, "")
lrt_res <- lrtest(ew_lrtn_m7, ew_lrtn_m8)
sprintf(
    "Likelihood-Ratio test p-value: %f (lrtest: %d; %f; %f)", 
    llike_rtest_pv,
    lrt_res$Df[2],
    lrt_res$Chisq[2],
    lrt_res$`Pr(>Chisq)`[2]
)
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“possible convergence problem: optim gave code = 1”
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = ew_lrtn_ts, order = c(1, 0, 0), seasonal = c(1, 0, 1), method = "ML")

Coefficients:
         ar1    sar1     sma1  intercept
      0.2292  0.9980  -0.9823     0.9786
s.e.  0.0456  0.0059   0.0264     0.6308

sigma^2 estimated as 27.93:  log likelihood = -1411.24,  aic = 2830.48

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.138352205071162
'Likelihood-Ratio test p-value: 0.000776 (lrtest: 1; 11.298865; 0.000776)'
No description has been provided for this image
In [745]:
ew_lrtn_m9 = arima(ew_lrtn_ts, order = c(0, 0, 1), seasonal = c(1, 0, 1), method = 'ML')
rsq = 1 - sum(ew_lrtn_m9$residuals^2)/sum(ew_lrtn^2)
summary(ew_lrtn_m9); rsq;
par(bg = 'white')
tsdiag1(ew_lrtn_m9, gof = 36)
# likelihood-ratio test
llike_rtest_pv <- pchisq(
    q = 2*(ew_lrtn_m9$loglik-ew_lrtn_m7$loglik),
    df = length(ew_lrtn_m9$coef)-length(ew_lrtn_m7$coef),
    lower.tail = FALSE
)
# cat("Likelihood-Ratio test p-value:", llike_rtest_pv, "")
lrt_res <- lrtest(ew_lrtn_m7, ew_lrtn_m9)
sprintf(
    "Likelihood-Ratio test p-value: %f (lrtest: %d; %f; %f)", 
    llike_rtest_pv,
    lrt_res$Df[2],
    lrt_res$Chisq[2],
    lrt_res$`Pr(>Chisq)`[2]
)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = ew_lrtn_ts, order = c(0, 0, 1), seasonal = c(1, 0, 1), method = "ML")

Coefficients:
         ma1   sar1     sma1  intercept
      0.2529  0.998  -0.9820     1.1667
s.e.  0.0461  0.006   0.0276     0.6214

sigma^2 estimated as 27.75:  log likelihood = -1409.94,  aic = 2827.88

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.144006818164794
'Likelihood-Ratio test p-value: 0.000193 (lrtest: 1; 13.898622; 0.000193)'
No description has been provided for this image

2-13 (c)

In [714]:
npts = 12
h = 12
eotr = length(ew_lrtn)-h
freq = 12
order = c(1,0,0)
ar1_fc_res1 <- plot_arima_forecast_fig(
    ew_lrtn_ts, eotr, h, npts, freq, gof=36, 
    order=order, method='ML'
)
ar1_fc_res2 <- plot_arima_forecast_fig(
    ew_lrtn_ts, length(ew_lrtn), h, npts, freq, gof=36, 
    order=order, method='ML'
)
[1] 444
1997.917 ; 2000
No description has been provided for this image
[1] 456
No description has been provided for this image
1998.917 ; 2001
No description has been provided for this image
No description has been provided for this image
In [713]:
npts = 12
h = 12
eotr = length(ew_lrtn)-h
freq = 12
order = c(1,0,0)
seasonal = list(order = c(1,0,0), period = freq)
ar2_fc_res1 <- plot_arima_forecast_fig(
    ew_lrtn_ts, eotr, h, npts, freq, gof=36, 
    order=order, seasonal=seasonal, method='ML'
)
ar2_fc_res2 <- plot_arima_forecast_fig(
    ew_lrtn_ts, length(ew_lrtn), h, npts, freq, gof=36, 
    order=order, seasonal=seasonal, method='ML'
)
[1] 444
1997.917 ; 2000
No description has been provided for this image
[1] 456
No description has been provided for this image
1998.917 ; 2001
No description has been provided for this image
No description has been provided for this image
In [715]:
npts = 12
h = 12
eotr = length(ew_lrtn)-h
freq = 12
order = c(0,0,1)
ma1_fc_res1 <- plot_arima_forecast_fig(
    ew_lrtn_ts, eotr, h, npts, freq, gof=36, 
    order=order, method='ML'
)
ma1_fc_res2 <- plot_arima_forecast_fig(
    ew_lrtn_ts, length(ew_lrtn), h, npts, freq, gof=36, 
    order=order, method='ML'
)
[1] 444
1997.917 ; 2000
No description has been provided for this image
[1] 456
No description has been provided for this image
1998.917 ; 2001
No description has been provided for this image
No description has been provided for this image
In [716]:
npts = 12
h = 12
eotr = length(ew_lrtn)-h
freq = 12
order = c(0,0,1)
seasonal = list(order = c(0,0,1), period = freq)
ma2_fc_res1 <- plot_arima_forecast_fig(
    ew_lrtn_ts, eotr, h, npts, freq, gof=36, 
    order=order, seasonal=seasonal, method='ML'
)
ma2_fc_res2 <- plot_arima_forecast_fig(
    ew_lrtn_ts, length(ew_lrtn), h, npts, freq, gof=36, 
    order=order, seasonal=seasonal, method='ML'
)
[1] 444
1997.917 ; 2000
No description has been provided for this image
[1] 456
No description has been provided for this image
1998.917 ; 2001
No description has been provided for this image
No description has been provided for this image
In [726]:
npts = 12
h = 12
eotr = length(ew_lrtn)-h
freq = 12
order = c(0,0,1)
seasonal = list(order = c(1,0,1), period = freq)
arma1_fc_res1 <- plot_arima_forecast_fig(
    ew_lrtn_ts, eotr, h, npts, freq, gof=36, 
    order=order, seasonal=seasonal, method='ML'
)
arma1_fc_res2 <- plot_arima_forecast_fig(
    ew_lrtn_ts, length(ew_lrtn), h, npts, freq, gof=36, 
    order=order, seasonal=seasonal, method='ML'
)
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“possible convergence problem: optim gave code = 1”
[1] 444
1997.917 ; 2000
No description has been provided for this image
[1] 456
No description has been provided for this image
1998.917 ; 2001
No description has been provided for this image
No description has been provided for this image

2-13 (d)

  • Likelihood ratio test.
In [741]:
print("1 vs 3")
lrtest(ew_lrtn_m1, ew_lrtn_m3);
print("2 vs 6")
lrtest(ew_lrtn_m2, ew_lrtn_m6);
print("3 vs 6")
lrtest(ew_lrtn_m3, ew_lrtn_m6);
print("3 vs 7")
lrtest(ew_lrtn_m3, ew_lrtn_m7);
print("6 vs 7")
lrtest(ew_lrtn_m6, ew_lrtn_m7);
print("7 vs 8")
lrtest(ew_lrtn_m7, ew_lrtn_m8);
print("1 vs 9")
lrtest(ew_lrtn_m1, ew_lrtn_m9);
print("3 vs 9")
lrtest(ew_lrtn_m3, ew_lrtn_m9);
print("7 vs 9")
lrtest(ew_lrtn_m7, ew_lrtn_m9);
print("8 vs 9")
lrtest(ew_lrtn_m8, ew_lrtn_m9);
[1] "1 vs 3"
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
13-1420.110NA NANA
23-1419.365 01.490312 0
[1] "2 vs 6"
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
14-1419.176NA NANA
24-1417.324 03.704036 0
[1] "3 vs 6"
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
13-1419.365NA NA NA
24-1417.324 14.0822790.04333517
[1] "3 vs 7"
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
13-1419.365NA NA NA
24-1416.889 14.9520980.02605903
[1] "6 vs 7"
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
14-1417.324NA NANA
24-1416.889 00.8698185 0
[1] "7 vs 8"
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
14-1416.889NA NA NA
25-1411.240 111.298860.0007755449
[1] "1 vs 9"
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
13-1420.11NA NA NA
25-1409.94 220.341033.828257e-05
[1] "3 vs 9"
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
13-1419.365NA NA NA
25-1409.940 218.850728.065257e-05
[1] "7 vs 9"
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
14-1416.889NA NA NA
25-1409.940 113.898620.0001929399
[1] "8 vs 9"
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
15-1411.24NA NANA
25-1409.94 02.599757 0
In [ ]:

2-14 (S&P 500 Spot and Futures)

  • This is a one-minute data. They are log-price.
  • Several authors used the data to study index futures arbitrage.
  • Interesting observations.
    • The lg(spot_rtn) has momentum in pacf and acf, but lg(fut_rtn) has no momentum in pacf and acf. This means the future is much more liquid than spot.
In [451]:
da = read.table("../AFTS_sol/data/sp5may.dat", header = T)
dim(da); da[1:5, ]
  1. 7061
  2. 3
A data.frame: 5 × 3
lnfuturelnspotcost
<dbl><dbl><dbl>
16.083826.08618-0.16501
26.084046.08623-0.16501
36.084736.08630-0.16501
46.084506.08630-0.16501
56.084506.08623-0.16501
In [452]:
lg_spt = da$lnspot
lg_fut = da$lnfuture
lg_spt_rtn = diff(lg_spt)
lg_fut_rtn = diff(lg_fut)
In [28]:
plot_time_fig(lg_spt, main = "Spot", xlab = "Minute")
plot_time_fig(lg_fut, main = "Futures", xlab = "Minute")
par(bg = 'white')
plot(lg_spt, type = 'l', col = 'blue', lty = 1)
lines(lg_fut, type = 'l', col = 'red', lty = 2)
legend("topleft", legend = c("Spot", "Futures"), col = c('blue', 'red'), lty = c(1, 2))
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [29]:
plot_time_fig(lg_spt_rtn, main = "Spot Return", xlab = "Minute")
plot_time_fig(lg_fut_rtn, main = "Futures Return", xlab = "Minute")
par(bg = 'white')
plot(lg_spt_rtn, type = 'l', col = 'blue', lty = 1)
lines(lg_fut_rtn, type = 'l', col = 'red', lty = 2)
legend("topleft", legend = c("Spot", "Futures"), col = c('blue', 'red'), lty = c(1, 2))
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [103]:
par(bg = 'white')
plot(lg_spt, lg_fut)
plot(lg_spt_rtn, lg_fut_rtn)
No description has been provided for this image
No description has been provided for this image
In [31]:
plot_pacf_acf(lg_spt)
No description has been provided for this image
No description has been provided for this image
In [32]:
plot_pacf_acf(lg_spt_rtn, main = 'Spot Rtn')
plot_pacf_acf(lg_fut_rtn, main = 'Fut Rtn')
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image

Simple linear model without time series errors

In [37]:
library(lmtest)
Loading required package: zoo


Attaching package: ‘zoo’


The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric


In [87]:
snp_m0 = lm(lg_fut_rtn~1+lg_spt_rtn)
rsq = 1 - sum(snp_m0$residuals^2)/sum(lg_fut_rtn^2)
summary(snp_m0); rsq
Call:
lm(formula = lg_fut_rtn ~ 1 + lg_spt_rtn)

Residuals:
       Min         1Q     Median         3Q        Max 
-0.0038484 -0.0001568 -0.0000014  0.0001612  0.0026256 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 1.354e-06  3.509e-06   0.386      0.7    
lg_spt_rtn  6.212e-01  1.754e-02  35.420   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.0002948 on 7058 degrees of freedom
Multiple R-squared:  0.1509,	Adjusted R-squared:  0.1508 
F-statistic:  1255 on 1 and 7058 DF,  p-value: < 2.2e-16
0.151011145451048
In [88]:
snp_m1 = lm(lg_fut_rtn~-1+lg_spt_rtn)
rsq = 1 - sum(snp_m1$residuals^2)/sum(lg_fut_rtn^2)
summary(snp_m1); rsq
Call:
lm(formula = lg_fut_rtn ~ -1 + lg_spt_rtn)

Residuals:
       Min         1Q     Median         3Q        Max 
-0.0038464 -0.0001554  0.0000000  0.0001626  0.0026271 

Coefficients:
           Estimate Std. Error t value Pr(>|t|)    
lg_spt_rtn  0.62132    0.01754   35.43   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.0002948 on 7059 degrees of freedom
Multiple R-squared:  0.151,	Adjusted R-squared:  0.1509 
F-statistic:  1255 on 1 and 7059 DF,  p-value: < 2.2e-16
0.150993243459798
In [57]:
lrtest(snp_m0, snp_m1)
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
1347374.79NA NA NA
2247374.71-10.14886740.6996201

Arima model with time series errors

  • eacf shows (1, 0, 1) as the arima order.
In [84]:
library(forecast)
In [144]:
perform_and_print_eacf(snp_lm1$residuals, ar.max = 25, ma.max = 12)
A data.frame: 26 × 13
0123456789101112
<I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>>
1-0.0641-0.056762-0.04879-0.05679-0.031148-0.03331-0.01152-0.01894-0.02451-0.01198-4.91e-03-0.02720-0.016396
2-0.5005-0.000725 0.00684-0.02186 0.012902-0.01930 0.01104-0.00149-0.01170-0.00160 1.95e-03-0.01700-0.009003
3-0.4994-0.046809 0.00194-0.01589-0.007687-0.00266 0.01268-0.00296-0.01111-0.00059 8.31e-03-0.01761 0.003340
4-0.4928-0.311891-0.20836-0.01290-0.002883-0.00108 0.01909-0.00572 0.00409-0.00227 5.85e-03-0.01639-0.013318
5-0.4691-0.441151 0.19523-0.05663 0.000758-0.00747 0.01489 0.00947 0.00466 0.00331-5.38e-04-0.01443-0.011757
6-0.4993-0.482765 0.09767-0.01969-0.357384-0.01333 0.01261 0.00497-0.00638 0.00188-5.55e-04-0.01087-0.010204
7-0.4436-0.484794-0.04477-0.20096-0.385600-0.25678 0.01326 0.00817 0.00072 0.00537-4.98e-04-0.00787 0.002959
8-0.4912-0.034750-0.42015-0.18953 0.171098-0.05583 0.10319 0.00351-0.00126 0.00524-1.99e-03-0.01001-0.003239
9-0.4955 0.057464-0.25791-0.27072 0.169440 0.08427-0.00764-0.35282-0.00973 0.00552-4.56e-04-0.00849-0.001256
10-0.4794-0.002704-0.22398-0.19924 0.287127 0.04632-0.01154-0.27957-0.15589 0.00499-2.23e-05-0.00776 0.001435
11-0.4799 0.001197-0.33184-0.28237-0.031639-0.15880-0.02490-0.17748-0.03422-0.03811-3.50e-03-0.01229-0.013387
12-0.4115-0.476866-0.49381-0.20266 0.035564-0.14865 0.06221-0.16996 0.00978-0.11507 2.83e-01-0.01029-0.011768
13-0.4910 0.102145 0.19541-0.34025 0.142312-0.11540-0.21242 0.12182-0.02005-0.21198 3.24e-01-0.23453-0.000747
14-0.4683 0.093940 0.17403-0.28533-0.174746-0.06871-0.08787 0.08983-0.02396-0.28858 1.64e-01-0.23532 0.024644
15 0.1279-0.206183-0.21281-0.24014 0.049505-0.20980-0.23956-0.07939-0.19401-0.28697-1.22e-01-0.24777-0.155502
16 0.3217-0.295826 0.04687-0.11287-0.154078-0.31311 0.13587 0.07873-0.24742-0.21214 2.56e-01-0.05692-0.161235
17-0.3139-0.243271-0.02828-0.06203-0.282482-0.25808 0.18181 0.10554-0.27500-0.18637 2.95e-01-0.03824-0.216724
18-0.0978-0.264445 0.05228-0.19380-0.403788-0.38631-0.28134-0.27010-0.06273-0.08613-1.09e-01-0.01248-0.167140
19-0.4997 0.300648-0.26448 0.12759-0.025345-0.14711 0.11395-0.23741 0.13555 0.01110-1.11e-01 0.01031-0.225348
20-0.1950-0.147715-0.27182 0.09416-0.023836-0.26940-0.07111-0.26063 0.09070 0.03415-5.80e-02 0.09132-0.199806
21-0.4883 0.045053-0.32521-0.08589-0.178063-0.16061 0.07635-0.13157-0.01117 0.14856 8.17e-02 0.03771-0.003578
22-0.3553-0.319356-0.18800 0.11991 0.067100-0.09262-0.08180-0.12797-0.01002 0.17447 5.96e-02 0.05300 0.001273
23-0.2339-0.484941-0.42140 0.12712 0.071142-0.01759-0.16475-0.01212-0.01695 0.13683-6.63e-02 0.00957-0.036909
24-0.2890-0.259142-0.03381-0.39325-0.044020 0.01628-0.15439 0.04627-0.00426 0.15843 1.07e-02 0.03615-0.037258
25-0.4977-0.171517 0.02005-0.00523-0.102941-0.02961-0.14872-0.03017-0.03862 0.02623-1.85e-02 0.08068 0.015314
26-0.4877-0.221424 0.03975-0.00213-0.121358 0.19844-0.14460-0.17895 0.08244 0.07663 5.66e-03 0.08257-0.011706
A matrix: 26 × 13 of type chr
0123456789101112
0xxxxxxooxooxo
1xoooooooooooo
2xxooooooooooo
3xxxoooooooooo
4xxxxooooooooo
5xxxoxoooooooo
6xxxxxxooooooo
7xxxxxxxoooooo
8xxxxxxoxooooo
9xoxxxxoxxoooo
10xoxxxxxxxxooo
11xxxxxxxxoxxoo
12xxxxxxxxoxxxo
13xxxxxxxxxxxxx
14xxxxxxxxxxxxx
15xxxxxxxxxxxxx
16xxxxxxxxxxxxx
17xxxxxxxxxxxox
18xxxxxxxxxoxox
19xxxxoxxxxxxxx
20xxxxxxxxoxxxo
21xxxxxxxxoxxxo
22xxxxxoxooxxox
23xxxxxoxxoxoxx
24xxooxxxxxxoxo
25xxxoxxxxxxoxo
0.023802777946289
AR/MA
   0 1 2 3 4 5 6 7 8 9 10 11 12
0  x x x x x x o o x o o  x  o 
1  x o o o o o o o o o o  o  o 
2  x x o o o o o o o o o  o  o 
3  x x x o o o o o o o o  o  o 
4  x x x x o o o o o o o  o  o 
5  x x x o x o o o o o o  o  o 
6  x x x x x x o o o o o  o  o 
7  x x x x x x x o o o o  o  o 
8  x x x x x x o x o o o  o  o 
9  x o x x x x o x x o o  o  o 
10 x o x x x x x x x x o  o  o 
11 x x x x x x x x o x x  o  o 
12 x x x x x x x x o x x  x  o 
13 x x x x x x x x x x x  x  x 
14 x x x x x x x x x x x  x  x 
15 x x x x x x x x x x x  x  x 
16 x x x x x x x x x x x  x  x 
17 x x x x x x x x x x x  o  x 
18 x x x x x x x x x o x  o  x 
19 x x x x o x x x x x x  x  x 
20 x x x x x x x x o x x  x  o 
21 x x x x x x x x o x x  x  o 
22 x x x x x o x o o x x  o  x 
23 x x x x x o x x o x o  x  x 
24 x x o o x x x x x x o  x  o 
25 x x x o x x x x x x o  x  o 
$eacf
             [,1]         [,2]         [,3]         [,4]          [,5]
 [1,] -0.06408465 -0.056762418 -0.048789444 -0.056785096 -0.0311484946
 [2,] -0.50051607 -0.000725284  0.006836699 -0.021855624  0.0129021516
 [3,] -0.49939185 -0.046808563  0.001944125 -0.015891674 -0.0076871174
 [4,] -0.49278437 -0.311891231 -0.208364022 -0.012904096 -0.0028826137
 [5,] -0.46909144 -0.441150766  0.195231225 -0.056631285  0.0007582789
 [6,] -0.49928259 -0.482765062  0.097670643 -0.019692672 -0.3573842607
 [7,] -0.44364011 -0.484794394 -0.044771039 -0.200963596 -0.3856004811
 [8,] -0.49118301 -0.034749568 -0.420150252 -0.189530841  0.1710976502
 [9,] -0.49546181  0.057463615 -0.257908216 -0.270717540  0.1694395633
[10,] -0.47944439 -0.002703975 -0.223977858 -0.199243498  0.2871273949
[11,] -0.47990221  0.001196895 -0.331836290 -0.282367322 -0.0316392623
[12,] -0.41153874 -0.476865926 -0.493805999 -0.202664462  0.0355640188
[13,] -0.49103755  0.102144794  0.195413690 -0.340248906  0.1423121636
[14,] -0.46830041  0.093940259  0.174032706 -0.285325993 -0.1747456831
[15,]  0.12789796 -0.206182924 -0.212813291 -0.240136606  0.0495049282
[16,]  0.32172867 -0.295826250  0.046868397 -0.112871181 -0.1540784281
[17,] -0.31385661 -0.243270916 -0.028283310 -0.062028068 -0.2824823150
[18,] -0.09784792 -0.264445382  0.052275518 -0.193796462 -0.4037877359
[19,] -0.49971173  0.300647678 -0.264475196  0.127586388 -0.0253449774
[20,] -0.19504951 -0.147715293 -0.271816392  0.094156928 -0.0238364359
[21,] -0.48833486  0.045052837 -0.325210290 -0.085887997 -0.1780629688
[22,] -0.35525678 -0.319355939 -0.187997131  0.119910188  0.0670997468
[23,] -0.23390619 -0.484941315 -0.421404536  0.127115254  0.0711417581
[24,] -0.28900827 -0.259142248 -0.033806440 -0.393248617 -0.0440201742
[25,] -0.49773392 -0.171517061  0.020047082 -0.005229804 -0.1029410535
[26,] -0.48772075 -0.221424261  0.039753317 -0.002132099 -0.1213576619
              [,6]         [,7]         [,8]          [,9]         [,10]
 [1,] -0.033307311 -0.011516692 -0.018943989 -0.0245085542 -0.0119814178
 [2,] -0.019296678  0.011036590 -0.001490419 -0.0117003055 -0.0015961815
 [3,] -0.002657878  0.012679027 -0.002960323 -0.0111108716 -0.0005900205
 [4,] -0.001079576  0.019087457 -0.005719725  0.0040899151 -0.0022663748
 [5,] -0.007467920  0.014893297  0.009466064  0.0046611966  0.0033059578
 [6,] -0.013329725  0.012611600  0.004973533 -0.0063824473  0.0018758289
 [7,] -0.256782009  0.013264866  0.008170167  0.0007200544  0.0053726839
 [8,] -0.055826334  0.103192930  0.003514260 -0.0012594031  0.0052371478
 [9,]  0.084266567 -0.007642043 -0.352815523 -0.0097313380  0.0055205700
[10,]  0.046319294 -0.011543839 -0.279566025 -0.1558871251  0.0049947867
[11,] -0.158804244 -0.024899097 -0.177478611 -0.0342242267 -0.0381146945
[12,] -0.148653372  0.062207736 -0.169960815  0.0097801945 -0.1150737801
[13,] -0.115401749 -0.212421811  0.121823970 -0.0200471744 -0.2119800801
[14,] -0.068710208 -0.087874824  0.089830372 -0.0239584422 -0.2885809823
[15,] -0.209801389 -0.239563490 -0.079391531 -0.1940084565 -0.2869746938
[16,] -0.313106118  0.135872988  0.078727274 -0.2474249863 -0.2121421844
[17,] -0.258080037  0.181805616  0.105540752 -0.2749975339 -0.1863699506
[18,] -0.386314606 -0.281342717 -0.270101194 -0.0627345167 -0.0861304569
[19,] -0.147109433  0.113949768 -0.237407570  0.1355450096  0.0111008878
[20,] -0.269399454 -0.071114087 -0.260633876  0.0906965746  0.0341474660
[21,] -0.160610182  0.076349918 -0.131572375 -0.0111668140  0.1485570135
[22,] -0.092622225 -0.081799617 -0.127970546 -0.0100192773  0.1744655642
[23,] -0.017589847 -0.164750257 -0.012123025 -0.0169522847  0.1368316901
[24,]  0.016278221 -0.154390298  0.046267877 -0.0042580959  0.1584293657
[25,] -0.029606783 -0.148715129 -0.030167439 -0.0386174773  0.0262333511
[26,]  0.198438667 -0.144602953 -0.178947571  0.0824428501  0.0766345853
              [,11]        [,12]         [,13]
 [1,] -4.910476e-03 -0.027203017 -0.0163957252
 [2,]  1.948885e-03 -0.016995416 -0.0090025294
 [3,]  8.310198e-03 -0.017611563  0.0033397822
 [4,]  5.851041e-03 -0.016392428 -0.0133176851
 [5,] -5.376093e-04 -0.014434772 -0.0117565706
 [6,] -5.546954e-04 -0.010867863 -0.0102040614
 [7,] -4.982022e-04 -0.007872970  0.0029587773
 [8,] -1.994912e-03 -0.010005443 -0.0032388078
 [9,] -4.559607e-04 -0.008493948 -0.0012561825
[10,] -2.226531e-05 -0.007761996  0.0014349144
[11,] -3.497342e-03 -0.012289822 -0.0133872572
[12,]  2.825917e-01 -0.010287270 -0.0117681653
[13,]  3.243088e-01 -0.234530288 -0.0007467272
[14,]  1.636999e-01 -0.235316527  0.0246443301
[15,] -1.216422e-01 -0.247769904 -0.1555023201
[16,]  2.557575e-01 -0.056923979 -0.1612353018
[17,]  2.954763e-01 -0.038236487 -0.2167236583
[18,] -1.086763e-01 -0.012475715 -0.1671396253
[19,] -1.108701e-01  0.010313670 -0.2253475321
[20,] -5.795444e-02  0.091316630 -0.1998064839
[21,]  8.172265e-02  0.037714340 -0.0035783670
[22,]  5.955171e-02  0.052999250  0.0012727768
[23,] -6.625591e-02  0.009569966 -0.0369091245
[24,]  1.068110e-02  0.036151810 -0.0372578352
[25,] -1.847852e-02  0.080684543  0.0153136619
[26,]  5.655412e-03  0.082566922 -0.0117058457

$ar.max
[1] 26

$ma.ma
[1] 13

$symbol
   0   1   2   3   4   5   6   7   8   9   10  11  12 
0  "x" "x" "x" "x" "x" "x" "o" "o" "x" "o" "o" "x" "o"
1  "x" "o" "o" "o" "o" "o" "o" "o" "o" "o" "o" "o" "o"
2  "x" "x" "o" "o" "o" "o" "o" "o" "o" "o" "o" "o" "o"
3  "x" "x" "x" "o" "o" "o" "o" "o" "o" "o" "o" "o" "o"
4  "x" "x" "x" "x" "o" "o" "o" "o" "o" "o" "o" "o" "o"
5  "x" "x" "x" "o" "x" "o" "o" "o" "o" "o" "o" "o" "o"
6  "x" "x" "x" "x" "x" "x" "o" "o" "o" "o" "o" "o" "o"
7  "x" "x" "x" "x" "x" "x" "x" "o" "o" "o" "o" "o" "o"
8  "x" "x" "x" "x" "x" "x" "o" "x" "o" "o" "o" "o" "o"
9  "x" "o" "x" "x" "x" "x" "o" "x" "x" "o" "o" "o" "o"
10 "x" "o" "x" "x" "x" "x" "x" "x" "x" "x" "o" "o" "o"
11 "x" "x" "x" "x" "x" "x" "x" "x" "o" "x" "x" "o" "o"
12 "x" "x" "x" "x" "x" "x" "x" "x" "o" "x" "x" "x" "o"
13 "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x"
14 "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x"
15 "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x"
16 "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x"
17 "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "o" "x"
18 "x" "x" "x" "x" "x" "x" "x" "x" "x" "o" "x" "o" "x"
19 "x" "x" "x" "x" "o" "x" "x" "x" "x" "x" "x" "x" "x"
20 "x" "x" "x" "x" "x" "x" "x" "x" "o" "x" "x" "x" "o"
21 "x" "x" "x" "x" "x" "x" "x" "x" "o" "x" "x" "x" "o"
22 "x" "x" "x" "x" "x" "o" "x" "o" "o" "x" "x" "o" "x"
23 "x" "x" "x" "x" "x" "o" "x" "x" "o" "x" "o" "x" "x"
24 "x" "x" "o" "o" "x" "x" "x" "x" "x" "x" "o" "x" "o"
25 "x" "x" "x" "o" "x" "x" "x" "x" "x" "x" "o" "x" "o"

$`0`
 [1] "-0.0641" "-0.5005" "-0.4994" "-0.4928" "-0.4691" "-0.4993" "-0.4436"
 [8] "-0.4912" "-0.4955" "-0.4794" "-0.4799" "-0.4115" "-0.4910" "-0.4683"
[15] " 0.1279" " 0.3217" "-0.3139" "-0.0978" "-0.4997" "-0.1950" "-0.4883"
[22] "-0.3553" "-0.2339" "-0.2890" "-0.4977" "-0.4877"

$`1`
 [1] "-0.056762" "-0.000725" "-0.046809" "-0.311891" "-0.441151" "-0.482765"
 [7] "-0.484794" "-0.034750" " 0.057464" "-0.002704" " 0.001197" "-0.476866"
[13] " 0.102145" " 0.093940" "-0.206183" "-0.295826" "-0.243271" "-0.264445"
[19] " 0.300648" "-0.147715" " 0.045053" "-0.319356" "-0.484941" "-0.259142"
[25] "-0.171517" "-0.221424"

$`2`
 [1] "-0.04879" " 0.00684" " 0.00194" "-0.20836" " 0.19523" " 0.09767"
 [7] "-0.04477" "-0.42015" "-0.25791" "-0.22398" "-0.33184" "-0.49381"
[13] " 0.19541" " 0.17403" "-0.21281" " 0.04687" "-0.02828" " 0.05228"
[19] "-0.26448" "-0.27182" "-0.32521" "-0.18800" "-0.42140" "-0.03381"
[25] " 0.02005" " 0.03975"

$`3`
 [1] "-0.05679" "-0.02186" "-0.01589" "-0.01290" "-0.05663" "-0.01969"
 [7] "-0.20096" "-0.18953" "-0.27072" "-0.19924" "-0.28237" "-0.20266"
[13] "-0.34025" "-0.28533" "-0.24014" "-0.11287" "-0.06203" "-0.19380"
[19] " 0.12759" " 0.09416" "-0.08589" " 0.11991" " 0.12712" "-0.39325"
[25] "-0.00523" "-0.00213"

$`4`
 [1] "-0.031148" " 0.012902" "-0.007687" "-0.002883" " 0.000758" "-0.357384"
 [7] "-0.385600" " 0.171098" " 0.169440" " 0.287127" "-0.031639" " 0.035564"
[13] " 0.142312" "-0.174746" " 0.049505" "-0.154078" "-0.282482" "-0.403788"
[19] "-0.025345" "-0.023836" "-0.178063" " 0.067100" " 0.071142" "-0.044020"
[25] "-0.102941" "-0.121358"

$`5`
 [1] "-0.03331" "-0.01930" "-0.00266" "-0.00108" "-0.00747" "-0.01333"
 [7] "-0.25678" "-0.05583" " 0.08427" " 0.04632" "-0.15880" "-0.14865"
[13] "-0.11540" "-0.06871" "-0.20980" "-0.31311" "-0.25808" "-0.38631"
[19] "-0.14711" "-0.26940" "-0.16061" "-0.09262" "-0.01759" " 0.01628"
[25] "-0.02961" " 0.19844"

$`6`
 [1] "-0.01152" " 0.01104" " 0.01268" " 0.01909" " 0.01489" " 0.01261"
 [7] " 0.01326" " 0.10319" "-0.00764" "-0.01154" "-0.02490" " 0.06221"
[13] "-0.21242" "-0.08787" "-0.23956" " 0.13587" " 0.18181" "-0.28134"
[19] " 0.11395" "-0.07111" " 0.07635" "-0.08180" "-0.16475" "-0.15439"
[25] "-0.14872" "-0.14460"

$`7`
 [1] "-0.01894" "-0.00149" "-0.00296" "-0.00572" " 0.00947" " 0.00497"
 [7] " 0.00817" " 0.00351" "-0.35282" "-0.27957" "-0.17748" "-0.16996"
[13] " 0.12182" " 0.08983" "-0.07939" " 0.07873" " 0.10554" "-0.27010"
[19] "-0.23741" "-0.26063" "-0.13157" "-0.12797" "-0.01212" " 0.04627"
[25] "-0.03017" "-0.17895"

$`8`
 [1] "-0.02451" "-0.01170" "-0.01111" " 0.00409" " 0.00466" "-0.00638"
 [7] " 0.00072" "-0.00126" "-0.00973" "-0.15589" "-0.03422" " 0.00978"
[13] "-0.02005" "-0.02396" "-0.19401" "-0.24742" "-0.27500" "-0.06273"
[19] " 0.13555" " 0.09070" "-0.01117" "-0.01002" "-0.01695" "-0.00426"
[25] "-0.03862" " 0.08244"

$`9`
 [1] "-0.01198" "-0.00160" "-0.00059" "-0.00227" " 0.00331" " 0.00188"
 [7] " 0.00537" " 0.00524" " 0.00552" " 0.00499" "-0.03811" "-0.11507"
[13] "-0.21198" "-0.28858" "-0.28697" "-0.21214" "-0.18637" "-0.08613"
[19] " 0.01110" " 0.03415" " 0.14856" " 0.17447" " 0.13683" " 0.15843"
[25] " 0.02623" " 0.07663"

$`10`
 [1] "-4.91e-03" " 1.95e-03" " 8.31e-03" " 5.85e-03" "-5.38e-04" "-5.55e-04"
 [7] "-4.98e-04" "-1.99e-03" "-4.56e-04" "-2.23e-05" "-3.50e-03" " 2.83e-01"
[13] " 3.24e-01" " 1.64e-01" "-1.22e-01" " 2.56e-01" " 2.95e-01" "-1.09e-01"
[19] "-1.11e-01" "-5.80e-02" " 8.17e-02" " 5.96e-02" "-6.63e-02" " 1.07e-02"
[25] "-1.85e-02" " 5.66e-03"

$`11`
 [1] "-0.02720" "-0.01700" "-0.01761" "-0.01639" "-0.01443" "-0.01087"
 [7] "-0.00787" "-0.01001" "-0.00849" "-0.00776" "-0.01229" "-0.01029"
[13] "-0.23453" "-0.23532" "-0.24777" "-0.05692" "-0.03824" "-0.01248"
[19] " 0.01031" " 0.09132" " 0.03771" " 0.05300" " 0.00957" " 0.03615"
[25] " 0.08068" " 0.08257"

$`12`
 [1] "-0.016396" "-0.009003" " 0.003340" "-0.013318" "-0.011757" "-0.010204"
 [7] " 0.002959" "-0.003239" "-0.001256" " 0.001435" "-0.013387" "-0.011768"
[13] "-0.000747" " 0.024644" "-0.155502" "-0.161235" "-0.216724" "-0.167140"
[19] "-0.225348" "-0.199806" "-0.003578" " 0.001273" "-0.036909" "-0.037258"
[25] " 0.015314" "-0.011706"
In [40]:
cat("order =", ar(snp_lm1$residuals, method = 'mle')$order, "\n")
order = 12 

Use full data set

In [89]:
snp_m2 = arima(lg_fut_rtn, order = c(1, 0, 0), xreg = lg_spt_rtn, include.mean = F, method = "ML")
rsq = 1 - sum(snp_m2$residuals^2)/sum(lg_fut_rtn^2)
summary(snp_m2); rsq;
lrtest(snp_m1, snp_m2)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_fut_rtn, order = c(1, 0, 0), xreg = lg_spt_rtn, include.mean = F, 
    method = "ML")

Coefficients:
          ar1    xreg
      -0.0641  0.6213
s.e.   0.0119  0.0173

sigma^2 estimated as 8.655e-08:  log likelihood = 47389.23,  aic = -94774.46

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.154478079419666
Warning message in modelUpdate(objects[[i - 1]], objects[[i]]):
“original model was of class "lm", updated model is of class "Arima"”
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
1247374.71NA NA NA
2347389.23 129.034027.111824e-08
In [90]:
snp_m3 = arima(lg_fut_rtn, order = c(0, 0, 1), xreg = lg_spt_rtn, include.mean = F, method = "ML")
rsq = 1 - sum(snp_m3$residuals^2)/sum(lg_fut_rtn^2)
summary(snp_m3); rsq;
lrtest(snp_m1, snp_m3)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_fut_rtn, order = c(0, 0, 1), xreg = lg_spt_rtn, include.mean = F, 
    method = "ML")

Coefficients:
          ma1    xreg
      -0.0737  0.6214
s.e.   0.0128  0.0173

sigma^2 estimated as 8.649e-08:  log likelihood = 47391.33,  aic = -94778.66

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.15498070702522
Warning message in modelUpdate(objects[[i - 1]], objects[[i]]):
“original model was of class "lm", updated model is of class "Arima"”
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
1247374.71NA NA NA
2347391.33 133.230818.184341e-09
In [91]:
snp_m4 = arima(lg_fut_rtn, order = c(12, 0, 0), xreg = lg_spt_rtn, include.mean = F, method = "ML")
rsq = 1 - sum(snp_m4$residuals^2)/sum(lg_fut_rtn^2)
summary(snp_m4); rsq;
lrtest(snp_m2, snp_m4)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_fut_rtn, order = c(12, 0, 0), xreg = lg_spt_rtn, include.mean = F, 
    method = "ML")

Coefficients:
          ar1      ar2      ar3      ar4      ar5      ar6      ar7      ar8
      -0.0924  -0.0922  -0.0889  -0.0957  -0.0737  -0.0743  -0.0513  -0.0558
s.e.   0.0119   0.0120   0.0122   0.0121   0.0122   0.0122   0.0121   0.0121
          ar9     ar10     ar11     ar12    xreg
      -0.0572  -0.0439  -0.0327  -0.0519  0.6889
s.e.   0.0121   0.0120   0.0120   0.0120  0.0165

sigma^2 estimated as 8.425e-08:  log likelihood = 47483.89,  aic = -94941.78

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.176869460888975
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
1 347389.23NA NA NA
21447483.8911189.31621.216971e-34
In [92]:
snp_m5 = arima(lg_fut_rtn, order = c(14, 0, 0), xreg = lg_spt_rtn, include.mean = F, method = "ML")
rsq = 1 - sum(snp_m5$residuals^2)/sum(lg_fut_rtn^2)
summary(snp_m5); rsq;
lrtest(snp_m4, snp_m5)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_fut_rtn, order = c(14, 0, 0), xreg = lg_spt_rtn, include.mean = F, 
    method = "ML")

Coefficients:
          ar1      ar2      ar3      ar4      ar5      ar6      ar7      ar8
      -0.0965  -0.0962  -0.0931  -0.1006  -0.0790  -0.0793  -0.0570  -0.0621
s.e.   0.0119   0.0121   0.0122   0.0122   0.0122   0.0122   0.0122   0.0122
          ar9     ar10     ar11     ar12     ar13     ar14    xreg
      -0.0644  -0.0515  -0.0401  -0.0596  -0.0462  -0.0303  0.6976
s.e.   0.0122   0.0122   0.0121   0.0121   0.0120   0.0119  0.0164

sigma^2 estimated as 8.402e-08:  log likelihood = 47493.61,  aic = -94957.21

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.179137098331569
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
11447483.89NA NA NA
21647493.61 219.435426.020777e-05
In [93]:
snp_m6 = arima(lg_fut_rtn, order = c(0, 0, 12), xreg = lg_spt_rtn, include.mean = F, method = "ML")
rsq = 1 - sum(snp_m6$residuals^2)/sum(lg_fut_rtn^2)
summary(snp_m6); rsq;
lrtest(snp_m4, snp_m6)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_fut_rtn, order = c(0, 0, 12), xreg = lg_spt_rtn, include.mean = F, 
    method = "ML")

Coefficients:
          ma1      ma2      ma3      ma4      ma5      ma6      ma7      ma8
      -0.1025  -0.0941  -0.0824  -0.0826  -0.0528  -0.0510  -0.0261  -0.0318
s.e.   0.0120   0.0121   0.0123   0.0122   0.0122   0.0122   0.0122   0.0121
          ma9     ma10     ma11     ma12    xreg
      -0.0353  -0.0222  -0.0104  -0.0347  0.7250
s.e.   0.0124   0.0119   0.0120   0.0120  0.0175

sigma^2 estimated as 8.378e-08:  log likelihood = 47503.66,  aic = -94981.33

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.181483127251869
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
11447483.89NA NANA
21447503.66 039.55018 0
In [94]:
snp_m7 = arima(lg_fut_rtn, order = c(0, 0, 10), xreg = lg_spt_rtn, include.mean = F, method = "ML")
rsq = 1 - sum(snp_m7$residuals^2)/sum(lg_fut_rtn^2)
summary(snp_m7); rsq;
lrtest(snp_m4, snp_m7);
lrtest(snp_m6, snp_m7);
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_fut_rtn, order = c(0, 0, 10), xreg = lg_spt_rtn, include.mean = F, 
    method = "ML")

Coefficients:
          ma1      ma2      ma3      ma4      ma5      ma6      ma7      ma8
      -0.1007  -0.0945  -0.0852  -0.0846  -0.0540  -0.0513  -0.0268  -0.0362
s.e.   0.0120   0.0122   0.0122   0.0121   0.0122   0.0126   0.0121   0.0120
          ma9     ma10    xreg
      -0.0408  -0.0274  0.7199
s.e.   0.0123   0.0118  0.0174

sigma^2 estimated as 8.39e-08:  log likelihood = 47498.76,  aic = -94975.52

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.180342168771766
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
11447483.89NA NA NA
21247498.76-229.739143.485196e-07
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
11447503.66NA NA NA
21247498.76-29.8110380.007405598
In [95]:
snp_m8 = arima(lg_fut_rtn, order = c(2, 0, 1), xreg = lg_spt_rtn, include.mean = F, method = "ML")
rsq = 1 - sum(snp_m8$residuals^2)/sum(lg_fut_rtn^2)
summary(snp_m8); rsq;
lrtest(snp_m6, snp_m8);
lrtest(snp_m7, snp_m8);
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_fut_rtn, order = c(2, 0, 1), xreg = lg_spt_rtn, include.mean = F, 
    method = "ML")

Coefficients:
         ar1      ar2      ma1    xreg
      0.8328  -0.0186  -0.9343  0.7271
s.e.  0.0145   0.0126   0.0088  0.0177

sigma^2 estimated as 8.386e-08:  log likelihood = 47500.18,  aic = -94992.35

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
0.180673004976664
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
11447503.66NA NA NA
2 547500.18-96.9764430.6395722
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
11247498.76NA NA NA
2 547500.18-72.8345950.8998702
In [200]:
snp_m9 = auto.arima(
    lg_fut_rtn,
    d = 0,
    D = 0,
    max.p = 3,
    max.q = 3,
    max.P = 0,
    max.Q = 0,
    max.order = 6,
    seasonal = TRUE,
    method = "ML",
    allowmean = TRUE,
    xreg = as.matrix(lg_spt_rtn),
    stepwise = FALSE,
    parallel = TRUE,
    num.cores = 4
)
rsq = 1 - sum(snp_m9$residuals^2)/sum(lg_fut_rtn^2)
summary(snp_m9); rsq;
lrtest(snp_m4, snp_m9);
lrtest(snp_m5, snp_m9);
lrtest(snp_m6, snp_m9);
lrtest(snp_m7, snp_m9);
lrtest(snp_m8, snp_m9);
Series: lg_fut_rtn 
Regression with ARIMA(2,0,1) errors 

Coefficients:
         ar1      ar2      ma1    xreg
      0.8328  -0.0186  -0.9343  0.7271
s.e.  0.0145   0.0126   0.0088  0.0177

sigma^2 = 8.391e-08:  log likelihood = 47500.18
AIC=-94990.35   AICc=-94990.34   BIC=-94956.04

Training set error measures:
                       ME         RMSE          MAE MPE MAPE      MASE
Training set 2.970033e-06 0.0002895942 0.0002081741 NaN  Inf 0.6506735
                      ACF1
Training set -0.0004151434
0.180673004976664
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
11447483.89NA NA NA
2 547500.18-932.573740.0001583986
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
11647493.61 NA NA NA
2 547500.18-1113.138320.2843811
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
11447503.66NA NA NA
2 547500.18-96.9764430.6395722
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
11247498.76NA NA NA
2 547500.18-72.8345950.8998702
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
1547500.18NANANA
2547500.18 0 0 1
In [142]:
snp_m9$loglik/length(lg_fut_rtn)
6.72807012204497
In [ ]:

Subsample data so that we focus more on larger magnitude of spot return

  • Seems like sub-sampling near the timestamps that are large in magnitude gives higher R-squared, even though here the R-squared may not be a rigorious metric to use.
In [129]:
spt_qtl_90 = quantile(abs(lg_spt_rtn), probs = 0.9)
spt_qtl_90
90%: 0.000220000000000553
In [139]:
byd_qtl_90_lg_spt_rtn_idx = which(abs(lg_spt_rtn)>spt_qtl_90)
byd_qtl_90_lg_spt_rtn_nei_idx = c()
for (i in 0:4) {
    byd_qtl_90_lg_spt_rtn_nei_idx = c(byd_qtl_90_lg_spt_rtn_nei_idx, byd_qtl_90_lg_spt_rtn_idx + i)
}
print(length(byd_qtl_90_lg_spt_rtn_nei_idx))
byd_qtl_90_lg_spt_rtn_nei_idx = sort(unique(byd_qtl_90_lg_spt_rtn_nei_idx))
print(length(byd_qtl_90_lg_spt_rtn_nei_idx))
new_lg_fut_rtn = lg_fut_rtn[byd_qtl_90_lg_spt_rtn_nei_idx]
new_lg_spt_rtn = lg_spt_rtn[byd_qtl_90_lg_spt_rtn_nei_idx]
[1] 3375
[1] 2147
In [141]:
snp_m10 = auto.arima(
    new_lg_fut_rtn,
    d = 0,
    D = 0,
    max.p = 3,
    max.q = 3,
    max.P = 0,
    max.Q = 0,
    max.order = 6,
    seasonal = TRUE,
    method = "ML",
    allowmean = TRUE,
    xreg = as.matrix(new_lg_spt_rtn),
    stepwise = FALSE,
    parallel = TRUE,
    num.cores = 4
)
rsq = 1 - sum(snp_m10$residuals^2)/sum(new_lg_fut_rtn^2)
summary(snp_m10); rsq;
Series: new_lg_fut_rtn 
Regression with ARIMA(2,0,1) errors 

Coefficients:
         ar1      ar2      ma1    xreg
      0.7144  -0.0707  -0.7387  0.6316
s.e.  0.0807   0.0241   0.0788  0.0235

sigma^2 = 1.415e-07:  log likelihood = 13885.55
AIC=-27761.1   AICc=-27761.07   BIC=-27732.74

Training set error measures:
                       ME         RMSE          MAE MPE MAPE      MASE
Training set 9.548472e-06 0.0003758318 0.0002728869 NaN  Inf 0.6664186
                      ACF1
Training set -0.0007842721
0.242582309349175
In [143]:
snp_m10$loglik/length(new_lg_fut_rtn)
6.46742001045078
In [ ]:

Forecasting

  • F1: Use the model fit with all data to forecast.
  • F2: Use the model fit with subsampled data to forecast.
  • The point-wise prediction are very close, b/c the coefs are close to each other.
  • The F2 gives larger CI than F1, b/c the $\hat{\sigma}^2$ and all the variance of estimates of coefs are larger for F2 than F1. This is b/c in fitting F2, a data set with smaller sample size is used.

Use full data set

In [100]:
# auto.arima
npts = 20
eotr = length(lg_fut_rtn)-npts
h = npts
freq = 1
xreg = as.matrix(lg_spt_rtn)
snp_fc_res = plot_auto_arima_forecast_fig(
    da_ts=lg_fut_rtn, eotr=eotr, h=h, npts=npts, frequency=freq,
    xreg=xreg,
    main="Forecasts from Regression with ARIMA(2,0,1) errors\nfor S&P 500",
    xlab="Minute", ylab="S&P 500 Minute Data", ylim=c(-0.001, 0.001),
    d = 0,
    D = 0,
    max.p = 3,
    max.q = 3,
    max.P = 0,
    max.Q = 0,
    max.order = 6,
    seasonal = TRUE,
    method = "ML",
    allowmean = TRUE,
    stepwise = FALSE,
    parallel = TRUE,
    num.cores = 12
)
[1] 7040
7020 ; 7061
No description has been provided for this image
No description has been provided for this image
In [101]:
snp_fc_tb = comb_forecast_res(snp_fc_res, lg_fut_rtn, eotr, freq)
snp_fc_tb
Forecast method: Regression with ARIMA(2,0,1) errors

Model Information:
Series: tr_da_ts 
Regression with ARIMA(2,0,1) errors 

Coefficients:
         ar1      ar2      ma1    xreg
      0.8319  -0.0191  -0.9334  0.7254
s.e.  0.0146   0.0126   0.0090  0.0177

sigma^2 = 8.404e-08:  log likelihood = 47360.47
AIC=-94710.94   AICc=-94710.93   BIC=-94676.64

Error measures:
                      ME        RMSE          MAE MPE MAPE      MASE
Training set 3.07126e-06 0.000289806 0.0002082859 NaN  Inf 0.6505268
                      ACF1
Training set -0.0002587163

Forecasts:
     Point Forecast         Lo 80        Hi 80         Lo 95        Hi 95
7041   2.160695e-05 -0.0003499000 0.0003931139 -0.0005465639 0.0005897777
7042   1.221152e-04 -0.0002512995 0.0004955299 -0.0004489733 0.0006932037
7043  -1.317707e-06 -0.0003767066 0.0003740711 -0.0005754254 0.0005727900
7044   7.703133e-05 -0.0002996573 0.0004537200 -0.0004990642 0.0006531269
7045  -5.608568e-05 -0.0004336214 0.0003214500 -0.0006334767 0.0005213053
7046  -1.248219e-04 -0.0005029100 0.0002532662 -0.0007030577 0.0004534140
7047   4.508050e-05 -0.0003333681 0.0004235291 -0.0005337067 0.0006238677
7048  -1.046565e-05 -0.0003891495 0.0003682183 -0.0005896127 0.0005686814
7049  -1.172624e-04 -0.0004961000 0.0002615751 -0.0006966445 0.0004621196
7050  -6.837605e-06 -0.0003857755 0.0003721003 -0.0005863732 0.0005726980
7051  -5.526790e-06 -0.0003845303 0.0003734767 -0.0005851626 0.0005741090
7052  -1.897435e-05 -0.0003980207 0.0003600720 -0.0005986757 0.0005607270
7053   6.167101e-05 -0.0003174033 0.0004407453 -0.0005180731 0.0006414151
7054  -1.044682e-04 -0.0004835608 0.0002746243 -0.0006842403 0.0004753038
7055  -2.359114e-06 -0.0003814636 0.0003767454 -0.0005821494 0.0005774312
7056   9.964273e-05 -0.0002794696 0.0004787550 -0.0004801595 0.0006794450
7057   1.072618e-04 -0.0002718556 0.0004863792 -0.0004725482 0.0006870719
7058  -5.202061e-05 -0.0004311414 0.0003271001 -0.0006318357 0.0005277945
7059  -8.079595e-05 -0.0004599189 0.0002983270 -0.0006606144 0.0004990225
7060   2.094668e-05 -0.0003581777 0.0004000710 -0.0005588739 0.0006007673
A Time Series:
  1. 2.16069476285051e-05
  2. 0.000122115178778826
  3. -1.31770715220593e-06
  4. 7.70313314579155e-05
  5. -5.60856775653177e-05
  6. -0.000124821858795663
  7. 4.50805013033269e-05
  8. -1.04656466756119e-05
  9. -0.000117262439180891
  10. -6.83760506987072e-06
  11. -5.52679027972588e-06
  12. -1.89743511132404e-05
  13. 6.16710140140426e-05
  14. -0.000104468221184575
  15. -2.35911370524389e-06
  16. 9.96427296374539e-05
  17. 0.000107261828504305
  18. -5.20206141144893e-05
  19. -8.07959490475821e-05
  20. 2.09466829286814e-05
A Time Series:
  1. 0.000289888388162229
  2. 0.000291377042737255
  3. 0.000292917476625792
  4. 0.00029393169675005
  5. 0.000294592671567093
  6. 0.000295023716178103
  7. 0.000295304994287642
  8. 0.000295488619366825
  9. 0.000295608527010805
  10. 0.000295686840973501
  11. 0.000295737995263681
  12. 0.000295771411528938
  13. 0.000295793241607227
  14. 0.000295807503157651
  15. 0.000295816820399683
  16. 0.000295822907549822
  17. 0.000295826884448904
  18. 0.000295829482679475
  19. 0.000295831180190015
  20. 0.00029583228923296
A Time Series:
  1. 0.000220000000000553
  2. 0.000230000000000175
  3. 0.000109999999999388
  4. 0.000110000000000277
  5. -0.000339999999999563
  6. 0.000119999999999898
  7. 0
  8. -0.000340000000000451
  9. -0.000110000000000277
  10. -0.000449999999999839
  11. 0
  12. -0.000109999999999388
  13. 0.000229999999999286
  14. 0.000110000000000277
  15. -0.000339999999999563
  16. 0.000229999999999286
  17. 0.000110000000000277
  18. 0.000110000000000277
  19. -0.000220000000000553
  20. -0.000229999999999286
A Time Series: 20 × 3
ForecastStd. ErrorActual
7041 2.160695e-050.0002898884 0.00022
7042 1.221152e-040.0002913770 0.00023
7043-1.317707e-060.0002929175 0.00011
7044 7.703133e-050.0002939317 0.00011
7045-5.608568e-050.0002945927-0.00034
7046-1.248219e-040.0002950237 0.00012
7047 4.508050e-050.0002953050 0.00000
7048-1.046565e-050.0002954886-0.00034
7049-1.172624e-040.0002956085-0.00011
7050-6.837605e-060.0002956868-0.00045
7051-5.526790e-060.0002957380 0.00000
7052-1.897435e-050.0002957714-0.00011
7053 6.167101e-050.0002957932 0.00023
7054-1.044682e-040.0002958075 0.00011
7055-2.359114e-060.0002958168-0.00034
7056 9.964273e-050.0002958229 0.00023
7057 1.072618e-040.0002958269 0.00011
7058-5.202061e-050.0002958295 0.00011
7059-8.079595e-050.0002958312-0.00022
7060 2.094668e-050.0002958323-0.00023
In [524]:
# forecast:Arima
npts = 20
eotr = length(lg_fut_rtn)-npts
h = npts
freq = 1
xreg = as.matrix(lg_spt_rtn)
order = c(2,0,1)
seas = NULL
normal_snp_fc_res = plot_arima_forecast_fig(
    da_ts=lg_fut_rtn, eotr=eotr, h=h, npts=npts, frequency=freq,
    xreg=xreg, gof=36,
    main=NULL, #"Forecasts from ARIMA(4,2,5)\nfor GDP Deflator",
    xlab="Minute", ylab="S&P 500 Minute Data", ylim=c(-0.001, 0.001),
    order=order, seasonal=seas, include.mean=F, method='ML'
)
# summary(normal_snp_fc_res) # Too much output
[1] 7040
7020 ; 7061
No description has been provided for this image
No description has been provided for this image
In [517]:
normal_snp_fc_tb = comb_forecast_res(normal_snp_fc_res, lg_fut_rtn, eotr, freq)
normal_snp_fc_tb
'The "summary(forecast_obj)" call output length 134275 exceeds the output limit 5000 so that it is suppressed.'
A Time Series:
  1. 2.16069476285051e-05
  2. 0.000122115178778826
  3. -1.31770715220593e-06
  4. 7.70313314579155e-05
  5. -5.60856775653177e-05
  6. -0.000124821858795663
  7. 4.50805013033269e-05
  8. -1.04656466756119e-05
  9. -0.000117262439180891
  10. -6.83760506987072e-06
  11. -5.52679027972588e-06
  12. -1.89743511132404e-05
  13. 6.16710140140426e-05
  14. -0.000104468221184575
  15. -2.35911370524389e-06
  16. 9.96427296374539e-05
  17. 0.000107261828504305
  18. -5.20206141144893e-05
  19. -8.07959490475821e-05
  20. 2.09466829286814e-05
A Time Series:
  1. 0.000289888388162229
  2. 0.000291377042737255
  3. 0.000292917476625792
  4. 0.00029393169675005
  5. 0.000294592671567093
  6. 0.000295023716178103
  7. 0.000295304994287642
  8. 0.000295488619366825
  9. 0.000295608527010805
  10. 0.000295686840973501
  11. 0.000295737995263681
  12. 0.000295771411528938
  13. 0.000295793241607227
  14. 0.000295807503157651
  15. 0.000295816820399683
  16. 0.000295822907549822
  17. 0.000295826884448904
  18. 0.000295829482679475
  19. 0.000295831180190015
  20. 0.00029583228923296
A Time Series:
  1. 0.000220000000000553
  2. 0.000230000000000175
  3. 0.000109999999999388
  4. 0.000110000000000277
  5. -0.000339999999999563
  6. 0.000119999999999898
  7. 0
  8. -0.000340000000000451
  9. -0.000110000000000277
  10. -0.000449999999999839
  11. 0
  12. -0.000109999999999388
  13. 0.000229999999999286
  14. 0.000110000000000277
  15. -0.000339999999999563
  16. 0.000229999999999286
  17. 0.000110000000000277
  18. 0.000110000000000277
  19. -0.000220000000000553
  20. -0.000229999999999286
A Time Series: 20 × 3
ForecastStd. ErrorActual
7041 2.160695e-050.0002898884 0.00022
7042 1.221152e-040.0002913770 0.00023
7043-1.317707e-060.0002929175 0.00011
7044 7.703133e-050.0002939317 0.00011
7045-5.608568e-050.0002945927-0.00034
7046-1.248219e-040.0002950237 0.00012
7047 4.508050e-050.0002953050 0.00000
7048-1.046565e-050.0002954886-0.00034
7049-1.172624e-040.0002956085-0.00011
7050-6.837605e-060.0002956868-0.00045
7051-5.526790e-060.0002957380 0.00000
7052-1.897435e-050.0002957714-0.00011
7053 6.167101e-050.0002957932 0.00023
7054-1.044682e-040.0002958075 0.00011
7055-2.359114e-060.0002958168-0.00034
7056 9.964273e-050.0002958229 0.00023
7057 1.072618e-040.0002958269 0.00011
7058-5.202061e-050.0002958295 0.00011
7059-8.079595e-050.0002958312-0.00022
7060 2.094668e-050.0002958323-0.00023
In [532]:
# forecast:Arima
npts = 20
eotr = length(lg_fut_rtn)-npts
h = npts
freq = 1
xreg = as.matrix(lg_spt_rtn)
order = c(2,0,1)
seas = NULL
sp_sz = length(lg_fut_rtn)
xreg = as.matrix(lg_spt_rtn[1:(sp_sz-npts)])
normal_snp_fm = Arima(
    lg_fut_rtn[1:(sp_sz-npts)], 
    order = order, 
    xreg = xreg, method = "ML", include.mean = F)
summary(normal_snp_fm)
fc_xreg = as.matrix(lg_spt_rtn[(sp_sz-npts+1):sp_sz])
forecast(normal_snp_fm, h = h, xreg = fc_xreg)
Series: lg_fut_rtn[1:(sp_sz - npts)] 
Regression with ARIMA(2,0,1) errors 

Coefficients:
         ar1      ar2      ma1    xreg
      0.8319  -0.0191  -0.9334  0.7254
s.e.  0.0146   0.0126   0.0090  0.0177

sigma^2 = 8.404e-08:  log likelihood = 47360.47
AIC=-94710.94   AICc=-94710.93   BIC=-94676.64

Training set error measures:
                      ME        RMSE          MAE MPE MAPE      MASE
Training set 3.07126e-06 0.000289806 0.0002082859 NaN  Inf 0.6505268
                      ACF1
Training set -0.0002587163
     Point Forecast         Lo 80        Hi 80         Lo 95        Hi 95
7041   2.160695e-05 -0.0003499000 0.0003931139 -0.0005465639 0.0005897777
7042   1.221152e-04 -0.0002512995 0.0004955299 -0.0004489733 0.0006932037
7043  -1.317707e-06 -0.0003767066 0.0003740711 -0.0005754254 0.0005727900
7044   7.703133e-05 -0.0002996573 0.0004537200 -0.0004990642 0.0006531269
7045  -5.608568e-05 -0.0004336214 0.0003214500 -0.0006334767 0.0005213053
7046  -1.248219e-04 -0.0005029100 0.0002532662 -0.0007030577 0.0004534140
7047   4.508050e-05 -0.0003333681 0.0004235291 -0.0005337067 0.0006238677
7048  -1.046565e-05 -0.0003891495 0.0003682183 -0.0005896127 0.0005686814
7049  -1.172624e-04 -0.0004961000 0.0002615751 -0.0006966445 0.0004621196
7050  -6.837605e-06 -0.0003857755 0.0003721003 -0.0005863732 0.0005726980
7051  -5.526790e-06 -0.0003845303 0.0003734767 -0.0005851626 0.0005741090
7052  -1.897435e-05 -0.0003980207 0.0003600720 -0.0005986757 0.0005607270
7053   6.167101e-05 -0.0003174033 0.0004407453 -0.0005180731 0.0006414151
7054  -1.044682e-04 -0.0004835608 0.0002746243 -0.0006842403 0.0004753038
7055  -2.359114e-06 -0.0003814636 0.0003767454 -0.0005821494 0.0005774312
7056   9.964273e-05 -0.0002794696 0.0004787550 -0.0004801595 0.0006794450
7057   1.072618e-04 -0.0002718556 0.0004863792 -0.0004725482 0.0006870719
7058  -5.202061e-05 -0.0004311414 0.0003271001 -0.0006318357 0.0005277945
7059  -8.079595e-05 -0.0004599189 0.0002983270 -0.0006606144 0.0004990225
7060   2.094668e-05 -0.0003581777 0.0004000710 -0.0005588739 0.0006007673

Subsample data so that we focus more on larger magnitude of spot return

In [271]:
attributes(snp_m10)
$names
  1. 'coef'
  2. 'sigma2'
  3. 'var.coef'
  4. 'mask'
  5. 'loglik'
  6. 'aic'
  7. 'arma'
  8. 'residuals'
  9. 'call'
  10. 'series'
  11. 'code'
  12. 'n.cond'
  13. 'nobs'
  14. 'model'
  15. 'bic'
  16. 'aicc'
  17. 'xreg'
  18. 'x'
  19. 'fitted'
$class
  1. 'forecast_ARIMA'
  2. 'ARIMA'
  3. 'Arima'
In [273]:
snp_m10_sub_spl = deep_copy(snp_fc_res$model)
snp_m10_sub_spl$coef = deep_copy(snp_m10$coef)
snp_m10_sub_spl$sigma2 = deep_copy(snp_m10$sigma2)
snp_m10_sub_spl$var.coef = deep_copy(snp_m10$var.coef)
In [287]:
display(snp_m10_sub_spl)
Series: tr_da_ts 
Regression with ARIMA(2,0,1) errors 

Coefficients:
         ar1      ar2      ma1    xreg
      0.7144  -0.0707  -0.7387  0.6316
s.e.  0.0807   0.0241   0.0788  0.0235

sigma^2 = 1.415e-07:  log likelihood = 47360.47
AIC=-94710.94   AICc=-94710.93   BIC=-94676.64
In [275]:
# Using the model fit with subsampled data
snp_sub_spl_ts_fc_res = forecast(
    snp_m10_sub_spl, 
    h = h, 
    xreg = as.matrix(lg_spt_rtn[(length(lg_spt_rtn)-h+1):length(lg_spt_rtn)])
)
snp_sub_spl_ts_fc_res
     Point Forecast         Lo 80        Hi 80         Lo 95        Hi 95
7041   1.316746e-05 -0.0004689296 0.0004952645 -0.0007241364 0.0007504713
7042   1.014853e-04 -0.0003830875 0.0005860581 -0.0006396048 0.0008425754
7043  -5.068591e-06 -0.0004922032 0.0004820660 -0.0007500766 0.0007399395
7044   6.390324e-05 -0.0004249180 0.0005527245 -0.0006836844 0.0008114909
7045  -5.139707e-05 -0.0005413176 0.0004385234 -0.0008006658 0.0006978717
7046  -1.107560e-04 -0.0006013934 0.0003798813 -0.0008611211 0.0006396090
7047   3.757873e-05 -0.0004535264 0.0005286839 -0.0007135017 0.0007886592
7048  -1.046565e-05 -0.0005018762 0.0004809449 -0.0007620132 0.0007410819
7049  -1.031966e-04 -0.0005948065 0.0003884133 -0.0008550491 0.0006486559
7050  -6.837605e-06 -0.0004985778 0.0004849026 -0.0007588893 0.0007452141
7051  -5.526790e-06 -0.0004973520 0.0004862984 -0.0007577086 0.0007466550
7052  -1.709891e-05 -0.0005089797 0.0004747819 -0.0007693657 0.0007351679
7053   5.323153e-05 -0.0004386856 0.0005451486 -0.0006990908 0.0008055538
7054  -9.134013e-05 -0.0005832809 0.0004006007 -0.0008436987 0.0006610184
7055  -2.359114e-06 -0.0004943154 0.0004895972 -0.0007547414 0.0007500231
7056   8.651464e-05 -0.0004054518 0.0005784811 -0.0006658831 0.0008389124
7057   9.319601e-05 -0.0003987770 0.0005851691 -0.0006592118 0.0008456039
7058  -4.545657e-05 -0.0005374339 0.0004465208 -0.0007978710 0.0007069579
7059  -7.048102e-05 -0.0005624612 0.0004214992 -0.0008228998 0.0006819378
7060   1.813352e-05 -0.0004738485 0.0005101156 -0.0007342881 0.0007705551
In [284]:
# Using the model fit with all data
display(snp_fc_res)
     Point Forecast         Lo 80        Hi 80         Lo 95        Hi 95
7041   2.160695e-05 -0.0003499000 0.0003931139 -0.0005465639 0.0005897777
7042   1.221152e-04 -0.0002512995 0.0004955299 -0.0004489733 0.0006932037
7043  -1.317707e-06 -0.0003767066 0.0003740711 -0.0005754254 0.0005727900
7044   7.703133e-05 -0.0002996573 0.0004537200 -0.0004990642 0.0006531269
7045  -5.608568e-05 -0.0004336214 0.0003214500 -0.0006334767 0.0005213053
7046  -1.248219e-04 -0.0005029100 0.0002532662 -0.0007030577 0.0004534140
7047   4.508050e-05 -0.0003333681 0.0004235291 -0.0005337067 0.0006238677
7048  -1.046565e-05 -0.0003891495 0.0003682183 -0.0005896127 0.0005686814
7049  -1.172624e-04 -0.0004961000 0.0002615751 -0.0006966445 0.0004621196
7050  -6.837605e-06 -0.0003857755 0.0003721003 -0.0005863732 0.0005726980
7051  -5.526790e-06 -0.0003845303 0.0003734767 -0.0005851626 0.0005741090
7052  -1.897435e-05 -0.0003980207 0.0003600720 -0.0005986757 0.0005607270
7053   6.167101e-05 -0.0003174033 0.0004407453 -0.0005180731 0.0006414151
7054  -1.044682e-04 -0.0004835608 0.0002746243 -0.0006842403 0.0004753038
7055  -2.359114e-06 -0.0003814636 0.0003767454 -0.0005821494 0.0005774312
7056   9.964273e-05 -0.0002794696 0.0004787550 -0.0004801595 0.0006794450
7057   1.072618e-04 -0.0002718556 0.0004863792 -0.0004725482 0.0006870719
7058  -5.202061e-05 -0.0004311414 0.0003271001 -0.0006318357 0.0005277945
7059  -8.079595e-05 -0.0004599189 0.0002983270 -0.0006606144 0.0004990225
7060   2.094668e-05 -0.0003581777 0.0004000710 -0.0005588739 0.0006007673
In [280]:
# Forecast using the model fit with subsampled data
draw_arima_forecast_fig(
    lg_fut_rtn, eotr=eotr, h=h, npts=npts, frequency=freq, ts_fc_res=snp_sub_spl_ts_fc_res,
    main="Using model that fitted using more weights on timestamps\nwith 90-percentile magnitude spot return", 
    xlab=NULL, ylab=NULL, ylim = c(-0.001, 0.001)
)
7020 ; 7061
No description has been provided for this image
In [286]:
# Forecast using the model fit with all data
draw_arima_forecast_fig(
    lg_fut_rtn, eotr=eotr, h=h, npts=npts, frequency=freq, ts_fc_res=snp_fc_res,
    main="Using model that fitted using more weights on timestamps\nwith all data (except the forecast part)", 
    xlab=NULL, ylab=NULL, ylim = c(-0.001, 0.001)
)
7020 ; 7061
No description has been provided for this image
In [ ]:

2-15 (Quarterly GDP Implicit Prce Deflator)

  • This is raw time series data.
  • Seems that need to take diff twice to get a stationary time series.
In [317]:
require(xts, TSA, lmtest)
In [290]:
da = read.table("../AFTS_sol/data/q-gdpdef.txt", header = T)
dim(da); da[1:5,]
  1. 248
  2. 4
A data.frame: 5 × 4
yearmomdaygdpdef
<int><int><int><dbl>
11947 1115.105
21947 4115.329
31947 7115.597
4194710115.989
51948 1116.111
In [309]:
gdpdef = da$gdpdef
gdpdef_ts = ts(gdpdef, frequency = 4, start = c(1947, 1))
diff_gdpdef = diff(gdpdef)
diff_gdpdef_ts = ts(diff_gdpdef, frequency = 4, start = c(1947, 2))
lg_gdpdef = log(gdpdef)
lg_gdpdef_ts = ts(lg_gdpdef, frequency = 4, start = c(1947, 1))
diff_lg_gdpdef = diff(lg_gdpdef)
diff_lg_gdpdef_ts = ts(diff_lg_gdpdef, frequency = 4, start = c(1947, 2))
In [292]:
plot_time_fig(gdpdef_ts, main = "GDP Deflator", xlab = "Year")
plot_time_fig(lg_gdpdef_ts, main = "log(GDP Deflator)", xlab = "Year")
No description has been provided for this image
No description has been provided for this image
In [310]:
plot_time_fig(diff_gdpdef_ts, main = "diff(GDP Deflator)", xlab = "Year")
plot_time_fig(diff_lg_gdpdef_ts, main = "diff(log(GDP Deflator))", xlab = "Year")
No description has been provided for this image
No description has been provided for this image
In [311]:
plot_time_fig(diff(diff_gdpdef_ts), main = "diff(diff(GDP Deflator))", xlab = "Year")
plot_time_fig(diff(diff_lg_gdpdef_ts), main = "diff(diff(log(GDP Deflator)))", xlab = "Year")
No description has been provided for this image
No description has been provided for this image
In [305]:
lag.max = 24
plot_pacf_acf(gdpdef, lag.max = lag.max, xlim = c(0, lag.max))
plot_pacf_acf(lg_gdpdef, lag.max = lag.max, xlim = c(0, lag.max))
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [306]:
lag.max = 24
plot_pacf_acf(diff(gdpdef), lag.max = lag.max, xlim = c(0, lag.max))
plot_pacf_acf(diff(lg_gdpdef), lag.max = lag.max, xlim = c(0, lag.max))
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [ ]:

Fit models

In [541]:
perform_and_print_eacf(diff_lg_gdpdef, ar.max = 25, ma.max = 12)
A data.frame: 26 × 13
0123456789101112
<I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>>
1 0.74162 0.65588 0.5940 0.50673 0.43036 0.38447 0.32892 0.3892 0.37155 0.39810 3.89e-01 0.410658 0.42881
2-0.30243-0.05283 0.0990 0.00288-0.06390 0.05597-0.18707 0.1573-0.08797 0.06221-5.00e-02-0.000145 0.11616
3-0.42733-0.18526 0.0987 0.00687-0.04825-0.02009-0.17155 0.0347 0.00602-0.01658-5.31e-02-0.002271 0.12392
4 0.27712 0.23251-0.0946 0.05080-0.02033-0.01217-0.19196 0.0502 0.00196-0.00363-3.06e-02 0.008800-0.00227
5-0.48915 0.32389 0.0724-0.02397-0.00713-0.01270-0.16842-0.0565-0.04341 0.00207-1.16e-02 0.038297 0.02837
6 0.50271 0.09098 0.0332-0.01458-0.08067-0.01216-0.17133 0.0462-0.01718-0.01765 2.31e-02-0.025758 0.00554
7 0.46265-0.02853 0.0910 0.02623-0.05455-0.02591-0.19084-0.0891-0.06008-0.03530-4.56e-02 0.042567 0.00622
8 0.10993-0.14041 0.1254 0.06915-0.31965-0.34188-0.16168 0.0229-0.03541 0.04354-1.12e-03-0.031724 0.01155
9 0.00294-0.38611 0.0905-0.12026-0.33769 0.28605-0.11215-0.1337-0.01920-0.03276-8.37e-05-0.028330-0.00575
10 0.01202-0.36202-0.1620-0.03502-0.20920 0.29103-0.12954-0.1176 0.00577-0.05632-9.73e-02-0.013268 0.03089
11 0.18158-0.44879 0.0682-0.08251-0.16815 0.05954-0.05557-0.0526-0.01907-0.04007-2.50e-02 0.004887 0.04539
12 0.24849-0.23180-0.3223-0.11625 0.02533 0.01112 0.00636-0.0543 0.01982-0.12496-1.54e-02 0.004017 0.03172
13-0.47964 0.35854-0.3493-0.14527 0.13638-0.01136-0.00178-0.2265 0.04276 0.00184 3.38e-02 0.018647 0.05203
14 0.48505 0.28973-0.1629-0.08906 0.00819-0.01957-0.08452-0.0818 0.00764-0.00637-3.59e-03 0.030108 0.01736
15 0.06914 0.25158-0.2905-0.03688-0.00409-0.23438-0.16251-0.1246 0.02418 0.04805-2.44e-02-0.046018-0.01063
16-0.06440 0.35054-0.1408-0.04508 0.07083-0.21567 0.09942-0.1543-0.07876-0.00111-4.71e-03 0.050863 0.00582
17 0.51868 0.00602-0.2612-0.10467 0.27572 0.11753-0.11139-0.2080-0.04991-0.05660 1.10e-02 0.000780 0.00321
18 0.31739 0.00211-0.3421-0.12613 0.10029 0.16983-0.01333-0.2718 0.11044 0.01143-4.99e-03 0.013047 0.00777
19-0.29753 0.30160-0.2063 0.18555-0.03750-0.05457 0.06175-0.0760 0.09887 0.03420 6.06e-02 0.013710 0.00933
20 0.25944 0.30921 0.1865 0.20225-0.02927-0.04850 0.18403 0.0445 0.10101 0.04947 4.49e-02-0.062652-0.01617
21-0.39039 0.26657-0.1141 0.26094 0.09986 0.04678 0.19859-0.1029 0.06972-0.01211 3.90e-02-0.071491-0.01213
22-0.14112-0.37569 0.0582 0.06233-0.13959-0.00205 0.20640 0.0244 0.06821-0.00425 5.58e-02-0.001094 0.02212
23-0.16003-0.19671 0.1921 0.01756-0.18701 0.00677 0.25058-0.0393 0.01220 0.14209 4.22e-02-0.012858 0.01585
24 0.48004 0.05838-0.0345-0.03553-0.31701-0.02891 0.30065-0.0787 0.03184 0.04083 1.96e-02-0.034436 0.02672
25 0.48367 0.02062-0.1220-0.21066-0.31862-0.03332 0.31341 0.1834 0.11387 0.00339 1.47e-02-0.079154 0.02582
26-0.40260 0.34273-0.1923 0.06596 0.05790-0.27896 0.37158-0.0963 0.12693-0.01055 7.38e-02-0.038879-0.08400
A matrix: 26 × 13 of type chr
0123456789101112
0xxxxxxxxxxxxx
1xoooooxxooooo
2xxooooxoooooo
3xxooooxoooooo
4xxooooxoooooo
5xoooooxoooooo
6xoooooxoooooo
7oxooxxxoooooo
8oxooxxoxooooo
9oxxoxxooooooo
10xxooxoooooooo
11xxxoooooooooo
12xxxxxooxooooo
13xxxoooooooooo
14oxxooxxoooooo
15oxxooxoxooooo
16xoxoxooxooooo
17xoxooxoxooooo
18xxxxooooooooo
19xxxxooxoooooo
20xxoxooxoooooo
21xxooxoxoooooo
22xxxoxoxooxooo
23xoooxoxoooooo
24xooxxoxxooooo
25xxxooxxoooooo
0.127256952595156
AR/MA
   0 1 2 3 4 5 6 7 8 9 10 11 12
0  x x x x x x x x x x x  x  x 
1  x o o o o o x x o o o  o  o 
2  x x o o o o x o o o o  o  o 
3  x x o o o o x o o o o  o  o 
4  x x o o o o x o o o o  o  o 
5  x o o o o o x o o o o  o  o 
6  x o o o o o x o o o o  o  o 
7  o x o o x x x o o o o  o  o 
8  o x o o x x o x o o o  o  o 
9  o x x o x x o o o o o  o  o 
10 x x o o x o o o o o o  o  o 
11 x x x o o o o o o o o  o  o 
12 x x x x x o o x o o o  o  o 
13 x x x o o o o o o o o  o  o 
14 o x x o o x x o o o o  o  o 
15 o x x o o x o x o o o  o  o 
16 x o x o x o o x o o o  o  o 
17 x o x o o x o x o o o  o  o 
18 x x x x o o o o o o o  o  o 
19 x x x x o o x o o o o  o  o 
20 x x o x o o x o o o o  o  o 
21 x x o o x o x o o o o  o  o 
22 x x x o x o x o o x o  o  o 
23 x o o o x o x o o o o  o  o 
24 x o o x x o x x o o o  o  o 
25 x x x o o x x o o o o  o  o 
$eacf
              [,1]         [,2]        [,3]         [,4]         [,5]
 [1,]  0.741623287  0.655882524  0.59400832  0.506734604  0.430364903
 [2,] -0.302432080 -0.052830612  0.09902602  0.002882219 -0.063895722
 [3,] -0.427329837 -0.185256160  0.09873815  0.006866826 -0.048254764
 [4,]  0.277115381  0.232511115 -0.09455158  0.050801928 -0.020326386
 [5,] -0.489148478  0.323887478  0.07244840 -0.023966375 -0.007132643
 [6,]  0.502711328  0.090975057  0.03322973 -0.014577811 -0.080666380
 [7,]  0.462654110 -0.028533406  0.09096287  0.026234843 -0.054545669
 [8,]  0.109933116 -0.140407884  0.12541036  0.069149747 -0.319646684
 [9,]  0.002944131 -0.386108558  0.09051534 -0.120261512 -0.337689941
[10,]  0.012019206 -0.362018332 -0.16202316 -0.035018039 -0.209201295
[11,]  0.181583038 -0.448788765  0.06820932 -0.082514745 -0.168145622
[12,]  0.248486486 -0.231798276 -0.32234380 -0.116253260  0.025334844
[13,] -0.479641228  0.358539254 -0.34931342 -0.145265454  0.136383244
[14,]  0.485051879  0.289729363 -0.16291382 -0.089058356  0.008185556
[15,]  0.069142115  0.251578445 -0.29051628 -0.036877547 -0.004091514
[16,] -0.064403419  0.350540645 -0.14076625 -0.045079901  0.070831041
[17,]  0.518677679  0.006017942 -0.26118156 -0.104668263  0.275723590
[18,]  0.317388333  0.002109041 -0.34213819 -0.126131941  0.100285718
[19,] -0.297531016  0.301604433 -0.20626171  0.185550857 -0.037499881
[20,]  0.259439690  0.309209259  0.18654019  0.202253454 -0.029269881
[21,] -0.390386947  0.266566217 -0.11405680  0.260935326  0.099862046
[22,] -0.141117583 -0.375692164  0.05818175  0.062329372 -0.139587063
[23,] -0.160033712 -0.196707573  0.19207847  0.017562324 -0.187010297
[24,]  0.480043498  0.058378726 -0.03445306 -0.035531555 -0.317012560
[25,]  0.483667666  0.020615412 -0.12203662 -0.210664566 -0.318619101
[26,] -0.402604228  0.342733017 -0.19232181  0.065957837  0.057899892
              [,6]         [,7]        [,8]         [,9]        [,10]
 [1,]  0.384472105  0.328916975  0.38918449  0.371554799  0.398100236
 [2,]  0.055965462 -0.187073702  0.15725786 -0.087969796  0.062210751
 [3,] -0.020085660 -0.171546538  0.03471340  0.006021245 -0.016581625
 [4,] -0.012173268 -0.191956804  0.05023509  0.001957571 -0.003627470
 [5,] -0.012695133 -0.168419813 -0.05653704 -0.043405196  0.002066608
 [6,] -0.012155396 -0.171325701  0.04618463 -0.017180268 -0.017652900
 [7,] -0.025913216 -0.190841424 -0.08912694 -0.060076126 -0.035300456
 [8,] -0.341875427 -0.161676939  0.02290657 -0.035410381  0.043543721
 [9,]  0.286045210 -0.112146378 -0.13371573 -0.019201760 -0.032755751
[10,]  0.291034073 -0.129537072 -0.11759962  0.005768055 -0.056323539
[11,]  0.059537700 -0.055567514 -0.05263329 -0.019071133 -0.040069714
[12,]  0.011116447  0.006360429 -0.05434854  0.019822670 -0.124960977
[13,] -0.011362886 -0.001777602 -0.22649443  0.042762333  0.001835554
[14,] -0.019573871 -0.084518533 -0.08180336  0.007635940 -0.006365035
[15,] -0.234375873 -0.162509972 -0.12462083  0.024183402  0.048048106
[16,] -0.215669646  0.099419860 -0.15428461 -0.078764056 -0.001105420
[17,]  0.117527944 -0.111392033 -0.20796693 -0.049908699 -0.056595055
[18,]  0.169834039 -0.013334079 -0.27180630  0.110435630  0.011434875
[19,] -0.054568983  0.061752417 -0.07596062  0.098867430  0.034201043
[20,] -0.048499745  0.184029306  0.04449578  0.101012710  0.049468189
[21,]  0.046783684  0.198586576 -0.10292678  0.069724013 -0.012109934
[22,] -0.002051910  0.206397455  0.02444044  0.068206866 -0.004251243
[23,]  0.006765775  0.250582897 -0.03926428  0.012200254  0.142091898
[24,] -0.028912603  0.300650150 -0.07869183  0.031837841  0.040827021
[25,] -0.033324996  0.313412157  0.18344254  0.113872669  0.003392061
[26,] -0.278955673  0.371582188 -0.09627393  0.126929782 -0.010549271
              [,11]         [,12]        [,13]
 [1,]  3.893306e-01  0.4106580484  0.428813838
 [2,] -4.998639e-02 -0.0001446280  0.116162851
 [3,] -5.306771e-02 -0.0022710745  0.123923245
 [4,] -3.059505e-02  0.0088003371 -0.002269328
 [5,] -1.161506e-02  0.0382969916  0.028370401
 [6,]  2.312928e-02 -0.0257583613  0.005543776
 [7,] -4.557695e-02  0.0425670051  0.006221653
 [8,] -1.123264e-03 -0.0317244664  0.011550430
 [9,] -8.373492e-05 -0.0283296136 -0.005749886
[10,] -9.733674e-02 -0.0132683394  0.030888694
[11,] -2.495950e-02  0.0048867415  0.045391246
[12,] -1.535744e-02  0.0040168531  0.031717035
[13,]  3.375343e-02  0.0186472427  0.052028794
[14,] -3.589724e-03  0.0301080006  0.017362618
[15,] -2.443998e-02 -0.0460177537 -0.010631492
[16,] -4.714100e-03  0.0508625606  0.005821677
[17,]  1.101531e-02  0.0007795066  0.003208747
[18,] -4.988764e-03  0.0130469751  0.007773244
[19,]  6.064453e-02  0.0137104983  0.009334978
[20,]  4.490345e-02 -0.0626522608 -0.016168855
[21,]  3.904620e-02 -0.0714905641 -0.012132873
[22,]  5.582804e-02 -0.0010944338  0.022124693
[23,]  4.217554e-02 -0.0128580359  0.015851520
[24,]  1.964797e-02 -0.0344364601  0.026721840
[25,]  1.470247e-02 -0.0791538757  0.025820634
[26,]  7.381882e-02 -0.0388787864 -0.084002928

$ar.max
[1] 26

$ma.ma
[1] 13

$symbol
   0   1   2   3   4   5   6   7   8   9   10  11  12 
0  "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x" "x"
1  "x" "o" "o" "o" "o" "o" "x" "x" "o" "o" "o" "o" "o"
2  "x" "x" "o" "o" "o" "o" "x" "o" "o" "o" "o" "o" "o"
3  "x" "x" "o" "o" "o" "o" "x" "o" "o" "o" "o" "o" "o"
4  "x" "x" "o" "o" "o" "o" "x" "o" "o" "o" "o" "o" "o"
5  "x" "o" "o" "o" "o" "o" "x" "o" "o" "o" "o" "o" "o"
6  "x" "o" "o" "o" "o" "o" "x" "o" "o" "o" "o" "o" "o"
7  "o" "x" "o" "o" "x" "x" "x" "o" "o" "o" "o" "o" "o"
8  "o" "x" "o" "o" "x" "x" "o" "x" "o" "o" "o" "o" "o"
9  "o" "x" "x" "o" "x" "x" "o" "o" "o" "o" "o" "o" "o"
10 "x" "x" "o" "o" "x" "o" "o" "o" "o" "o" "o" "o" "o"
11 "x" "x" "x" "o" "o" "o" "o" "o" "o" "o" "o" "o" "o"
12 "x" "x" "x" "x" "x" "o" "o" "x" "o" "o" "o" "o" "o"
13 "x" "x" "x" "o" "o" "o" "o" "o" "o" "o" "o" "o" "o"
14 "o" "x" "x" "o" "o" "x" "x" "o" "o" "o" "o" "o" "o"
15 "o" "x" "x" "o" "o" "x" "o" "x" "o" "o" "o" "o" "o"
16 "x" "o" "x" "o" "x" "o" "o" "x" "o" "o" "o" "o" "o"
17 "x" "o" "x" "o" "o" "x" "o" "x" "o" "o" "o" "o" "o"
18 "x" "x" "x" "x" "o" "o" "o" "o" "o" "o" "o" "o" "o"
19 "x" "x" "x" "x" "o" "o" "x" "o" "o" "o" "o" "o" "o"
20 "x" "x" "o" "x" "o" "o" "x" "o" "o" "o" "o" "o" "o"
21 "x" "x" "o" "o" "x" "o" "x" "o" "o" "o" "o" "o" "o"
22 "x" "x" "x" "o" "x" "o" "x" "o" "o" "x" "o" "o" "o"
23 "x" "o" "o" "o" "x" "o" "x" "o" "o" "o" "o" "o" "o"
24 "x" "o" "o" "x" "x" "o" "x" "x" "o" "o" "o" "o" "o"
25 "x" "x" "x" "o" "o" "x" "x" "o" "o" "o" "o" "o" "o"

$`0`
 [1] " 0.74162" "-0.30243" "-0.42733" " 0.27712" "-0.48915" " 0.50271"
 [7] " 0.46265" " 0.10993" " 0.00294" " 0.01202" " 0.18158" " 0.24849"
[13] "-0.47964" " 0.48505" " 0.06914" "-0.06440" " 0.51868" " 0.31739"
[19] "-0.29753" " 0.25944" "-0.39039" "-0.14112" "-0.16003" " 0.48004"
[25] " 0.48367" "-0.40260"

$`1`
 [1] " 0.65588" "-0.05283" "-0.18526" " 0.23251" " 0.32389" " 0.09098"
 [7] "-0.02853" "-0.14041" "-0.38611" "-0.36202" "-0.44879" "-0.23180"
[13] " 0.35854" " 0.28973" " 0.25158" " 0.35054" " 0.00602" " 0.00211"
[19] " 0.30160" " 0.30921" " 0.26657" "-0.37569" "-0.19671" " 0.05838"
[25] " 0.02062" " 0.34273"

$`2`
 [1] " 0.5940" " 0.0990" " 0.0987" "-0.0946" " 0.0724" " 0.0332" " 0.0910"
 [8] " 0.1254" " 0.0905" "-0.1620" " 0.0682" "-0.3223" "-0.3493" "-0.1629"
[15] "-0.2905" "-0.1408" "-0.2612" "-0.3421" "-0.2063" " 0.1865" "-0.1141"
[22] " 0.0582" " 0.1921" "-0.0345" "-0.1220" "-0.1923"

$`3`
 [1] " 0.50673" " 0.00288" " 0.00687" " 0.05080" "-0.02397" "-0.01458"
 [7] " 0.02623" " 0.06915" "-0.12026" "-0.03502" "-0.08251" "-0.11625"
[13] "-0.14527" "-0.08906" "-0.03688" "-0.04508" "-0.10467" "-0.12613"
[19] " 0.18555" " 0.20225" " 0.26094" " 0.06233" " 0.01756" "-0.03553"
[25] "-0.21066" " 0.06596"

$`4`
 [1] " 0.43036" "-0.06390" "-0.04825" "-0.02033" "-0.00713" "-0.08067"
 [7] "-0.05455" "-0.31965" "-0.33769" "-0.20920" "-0.16815" " 0.02533"
[13] " 0.13638" " 0.00819" "-0.00409" " 0.07083" " 0.27572" " 0.10029"
[19] "-0.03750" "-0.02927" " 0.09986" "-0.13959" "-0.18701" "-0.31701"
[25] "-0.31862" " 0.05790"

$`5`
 [1] " 0.38447" " 0.05597" "-0.02009" "-0.01217" "-0.01270" "-0.01216"
 [7] "-0.02591" "-0.34188" " 0.28605" " 0.29103" " 0.05954" " 0.01112"
[13] "-0.01136" "-0.01957" "-0.23438" "-0.21567" " 0.11753" " 0.16983"
[19] "-0.05457" "-0.04850" " 0.04678" "-0.00205" " 0.00677" "-0.02891"
[25] "-0.03332" "-0.27896"

$`6`
 [1] " 0.32892" "-0.18707" "-0.17155" "-0.19196" "-0.16842" "-0.17133"
 [7] "-0.19084" "-0.16168" "-0.11215" "-0.12954" "-0.05557" " 0.00636"
[13] "-0.00178" "-0.08452" "-0.16251" " 0.09942" "-0.11139" "-0.01333"
[19] " 0.06175" " 0.18403" " 0.19859" " 0.20640" " 0.25058" " 0.30065"
[25] " 0.31341" " 0.37158"

$`7`
 [1] " 0.3892" " 0.1573" " 0.0347" " 0.0502" "-0.0565" " 0.0462" "-0.0891"
 [8] " 0.0229" "-0.1337" "-0.1176" "-0.0526" "-0.0543" "-0.2265" "-0.0818"
[15] "-0.1246" "-0.1543" "-0.2080" "-0.2718" "-0.0760" " 0.0445" "-0.1029"
[22] " 0.0244" "-0.0393" "-0.0787" " 0.1834" "-0.0963"

$`8`
 [1] " 0.37155" "-0.08797" " 0.00602" " 0.00196" "-0.04341" "-0.01718"
 [7] "-0.06008" "-0.03541" "-0.01920" " 0.00577" "-0.01907" " 0.01982"
[13] " 0.04276" " 0.00764" " 0.02418" "-0.07876" "-0.04991" " 0.11044"
[19] " 0.09887" " 0.10101" " 0.06972" " 0.06821" " 0.01220" " 0.03184"
[25] " 0.11387" " 0.12693"

$`9`
 [1] " 0.39810" " 0.06221" "-0.01658" "-0.00363" " 0.00207" "-0.01765"
 [7] "-0.03530" " 0.04354" "-0.03276" "-0.05632" "-0.04007" "-0.12496"
[13] " 0.00184" "-0.00637" " 0.04805" "-0.00111" "-0.05660" " 0.01143"
[19] " 0.03420" " 0.04947" "-0.01211" "-0.00425" " 0.14209" " 0.04083"
[25] " 0.00339" "-0.01055"

$`10`
 [1] " 3.89e-01" "-5.00e-02" "-5.31e-02" "-3.06e-02" "-1.16e-02" " 2.31e-02"
 [7] "-4.56e-02" "-1.12e-03" "-8.37e-05" "-9.73e-02" "-2.50e-02" "-1.54e-02"
[13] " 3.38e-02" "-3.59e-03" "-2.44e-02" "-4.71e-03" " 1.10e-02" "-4.99e-03"
[19] " 6.06e-02" " 4.49e-02" " 3.90e-02" " 5.58e-02" " 4.22e-02" " 1.96e-02"
[25] " 1.47e-02" " 7.38e-02"

$`11`
 [1] " 0.410658" "-0.000145" "-0.002271" " 0.008800" " 0.038297" "-0.025758"
 [7] " 0.042567" "-0.031724" "-0.028330" "-0.013268" " 0.004887" " 0.004017"
[13] " 0.018647" " 0.030108" "-0.046018" " 0.050863" " 0.000780" " 0.013047"
[19] " 0.013710" "-0.062652" "-0.071491" "-0.001094" "-0.012858" "-0.034436"
[25] "-0.079154" "-0.038879"

$`12`
 [1] " 0.42881" " 0.11616" " 0.12392" "-0.00227" " 0.02837" " 0.00554"
 [7] " 0.00622" " 0.01155" "-0.00575" " 0.03089" " 0.04539" " 0.03172"
[13] " 0.05203" " 0.01736" "-0.01063" " 0.00582" " 0.00321" " 0.00777"
[19] " 0.00933" "-0.01617" "-0.01213" " 0.02212" " 0.01585" " 0.02672"
[25] " 0.02582" "-0.08400"
In [314]:
perform_and_print_eacf(diff(diff_lg_gdpdef), ar.max = 25, ma.max = 12)
A data.frame: 26 × 13
0123456789101112
<I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>><I<chr>>
1-0.3323-0.064254 0.0742-0.02307-0.07446 0.03919-0.21311 0.1550-0.09100 0.061637-0.05174-0.001235 0.10972
2-0.4488-0.281984 0.0612-0.01690-0.08694-0.02063-0.17381 0.0355 0.00660-0.016491-0.05473-0.002955 0.12035
3-0.0628-0.061023-0.1803-0.03125-0.01887-0.00792-0.19194 0.0533 0.00255-0.003969-0.03104 0.008689-0.00154
4-0.4893 0.000724-0.2533 0.06140-0.01071-0.01576-0.16926-0.0614-0.04459 0.001634-0.01178 0.035551 0.02839
5-0.1195-0.105819-0.0293 0.01066-0.08406-0.01407-0.17311 0.0438-0.01788-0.019203 0.02321-0.025939 0.00498
6-0.2041-0.298158-0.0427 0.00602-0.04809-0.01331-0.19219-0.0893-0.06020-0.035562-0.04460 0.039859 0.00560
7-0.0651-0.286178-0.0128-0.04064-0.38405-0.34808-0.16193 0.0222-0.03731 0.041164-0.00195-0.032196 0.01025
8-0.0875-0.397286 0.0726-0.12508-0.34040 0.26982-0.11468-0.1297-0.03475-0.030437 0.00202-0.030755-0.00704
9-0.1608-0.385927-0.1664-0.03544-0.21080 0.27805-0.14352-0.1482 0.01797-0.057069-0.10002-0.013912 0.03177
10 0.0176-0.459485 0.0872-0.07931-0.19021 0.01808-0.07198-0.0551-0.01567-0.042907-0.02958 0.001953 0.04602
11 0.0312-0.250678-0.3259-0.12090 0.02498 0.00570 0.00785-0.0560 0.01623-0.130042-0.01820 0.000779 0.03548
12-0.4877 0.311917-0.3816-0.18384 0.11310-0.00656-0.00384-0.2247 0.03866-0.000642 0.03262 0.016040 0.05729
13 0.4583 0.160091-0.2967-0.19943 0.04036-0.00757-0.07667-0.0981 0.00311-0.006330-0.00340 0.029735 0.01733
14 0.2225 0.130742-0.3532-0.08096-0.00223-0.28050-0.16300-0.1304 0.00730 0.081661-0.01843-0.047763-0.01306
15-0.1809 0.387263-0.3003-0.06807 0.00393-0.23743 0.09890-0.1553-0.03011-0.002795-0.00939 0.049894 0.00439
16 0.4569-0.107219-0.2993-0.11328 0.23281 0.10691-0.13553-0.2114-0.01788-0.056317 0.01275-0.001000-0.04115
17 0.3528-0.051568-0.3720-0.12100 0.10925 0.15160-0.02097-0.2720 0.11537 0.025506-0.00549-0.003701-0.05174
18-0.3268 0.288669-0.2296 0.17717-0.05106-0.05458 0.05696-0.0897 0.09886 0.031352 0.05107-0.002508-0.00625
19 0.2028 0.265537 0.1001 0.19180-0.03448-0.05619 0.17346 0.0331 0.08485 0.026658 0.01140-0.098519-0.01769
20-0.3861 0.289993-0.0771 0.15601 0.04076 0.04810 0.20607-0.0867 0.07186 0.006550 0.00533-0.070870-0.01531
21 0.0186-0.348929 0.0713 0.11378-0.05971 0.00829 0.20696 0.0310 0.08284-0.013591 0.03121-0.030836 0.01219
22 0.0452-0.090343 0.2195 0.02273-0.13161 0.03503 0.25450-0.0472 0.01309 0.168967 0.04630-0.021403 0.01338
23 0.3942 0.182563-0.0151-0.04469-0.30377-0.02133 0.31137-0.0800 0.02970 0.059284 0.06577 0.002126 0.02794
24 0.5005 0.121115-0.0207-0.16626-0.30765-0.02536 0.32928 0.1783 0.12151-0.026101 0.05645-0.063977 0.02826
25-0.3965 0.368508-0.1879 0.07636 0.07359-0.27685 0.38897-0.0862 0.13942 0.018713 0.08665-0.033828-0.07825
26-0.2197 0.281254-0.0615 0.07482 0.06358-0.14492 0.13357-0.3021 0.16831-0.104082 0.09378-0.037294-0.02163
A matrix: 26 × 13 of type chr
0123456789101112
0xoooooxxooooo
1xxooooxoooooo
2ooxoooxoooooo
3xoxoooxoooooo
4ooooooxoooooo
5xxooooxoooooo
6oxooxxxoooooo
7oxooxxooooooo
8xxxoxxxxooooo
9oxooxoooooooo
10oxxoooooooooo
11xxxxoooxooooo
12xxxxooooooooo
13xoxooxxoooooo
14xxxooxoxooooo
15xoxoxoxxooooo
16xoxooxoxooooo
17xxxxooooooooo
18xxoxooxoooooo
19xxoxooxoooooo
20oxooooxoooooo
21ooxoooxooxooo
22xxooxoxoooooo
23xooxxoxxooooo
24xxxooxxoxoooo
25xxoooxoxxoooo
0.127515342612668
AR/MA
   0 1 2 3 4 5 6 7 8 9 10 11 12
0  x o o o o o x x o o o  o  o 
1  x x o o o o x o o o o  o  o 
2  o o x o o o x o o o o  o  o 
3  x o x o o o x o o o o  o  o 
4  o o o o o o x o o o o  o  o 
5  x x o o o o x o o o o  o  o 
6  o x o o x x x o o o o  o  o 
7  o x o o x x o o o o o  o  o 
8  x x x o x x x x o o o  o  o 
9  o x o o x o o o o o o  o  o 
10 o x x o o o o o o o o  o  o 
11 x x x x o o o x o o o  o  o 
12 x x x x o o o o o o o  o  o 
13 x o x o o x x o o o o  o  o 
14 x x x o o x o x o o o  o  o 
15 x o x o x o x x o o o  o  o 
16 x o x o o x o x o o o  o  o 
17 x x x x o o o o o o o  o  o 
18 x x o x o o x o o o o  o  o 
19 x x o x o o x o o o o  o  o 
20 o x o o o o x o o o o  o  o 
21 o o x o o o x o o x o  o  o 
22 x x o o x o x o o o o  o  o 
23 x o o x x o x x o o o  o  o 
24 x x x o o x x o x o o  o  o 
25 x x o o o x o x x o o  o  o 
$eacf
             [,1]          [,2]        [,3]         [,4]         [,5]
 [1,] -0.33226730 -0.0642542523  0.07420156 -0.023074575 -0.074464896
 [2,] -0.44879239 -0.2819844115  0.06117964 -0.016898816 -0.086942979
 [3,] -0.06275779 -0.0610228234 -0.18030769 -0.031248354 -0.018868343
 [4,] -0.48927176  0.0007241339 -0.25332083  0.061395915 -0.010710771
 [5,] -0.11947622 -0.1058191086 -0.02934818  0.010661105 -0.084059106
 [6,] -0.20408953 -0.2981583296 -0.04272794  0.006020465 -0.048088438
 [7,] -0.06514816 -0.2861777573 -0.01283275 -0.040642424 -0.384050069
 [8,] -0.08749862 -0.3972855774  0.07263700 -0.125083974 -0.340401555
 [9,] -0.16079080 -0.3859269037 -0.16643861 -0.035441726 -0.210802727
[10,]  0.01762000 -0.4594847092  0.08717079 -0.079308778 -0.190206993
[11,]  0.03120163 -0.2506777516 -0.32594770 -0.120899736  0.024979308
[12,] -0.48771994  0.3119166180 -0.38157701 -0.183844362  0.113097638
[13,]  0.45831969  0.1600908277 -0.29665421 -0.199429546  0.040361690
[14,]  0.22253497  0.1307415940 -0.35321076 -0.080957371 -0.002229446
[15,] -0.18087290  0.3872632155 -0.30025817 -0.068071461  0.003926386
[16,]  0.45688897 -0.1072186541 -0.29926294 -0.113276276  0.232813662
[17,]  0.35281146 -0.0515680996 -0.37200611 -0.121002472  0.109252264
[18,] -0.32676745  0.2886693496 -0.22958539  0.177170555 -0.051062135
[19,]  0.20280805  0.2655370824  0.10010287  0.191804666 -0.034476790
[20,] -0.38611992  0.2899932871 -0.07711009  0.156011236  0.040758814
[21,]  0.01859320 -0.3489293647  0.07131446  0.113778165 -0.059708057
[22,]  0.04519671 -0.0903427895  0.21951490  0.022725730 -0.131612831
[23,]  0.39420663  0.1825628055 -0.01505658 -0.044694214 -0.303771998
[24,]  0.50053571  0.1211154718 -0.02074212 -0.166264796 -0.307651474
[25,] -0.39651144  0.3685078890 -0.18785561  0.076356360  0.073592106
[26,] -0.21969763  0.2812535904 -0.06146567  0.074822613  0.063583740
              [,6]         [,7]        [,8]         [,9]         [,10]
 [1,]  0.039194663 -0.213114436  0.15502629 -0.091004860  0.0616367497
 [2,] -0.020631098 -0.173806957  0.03550043  0.006602094 -0.0164910744
 [3,] -0.007918206 -0.191940877  0.05333706  0.002548113 -0.0039686951
 [4,] -0.015760871 -0.169259943 -0.06144318 -0.044594543  0.0016338030
 [5,] -0.014072497 -0.173106086  0.04378054 -0.017883643 -0.0192028855
 [6,] -0.013310466 -0.192190826 -0.08929992 -0.060198864 -0.0355617385
 [7,] -0.348079448 -0.161934545  0.02215759 -0.037307364  0.0411636360
 [8,]  0.269823444 -0.114678989 -0.12970206 -0.034754295 -0.0304374268
 [9,]  0.278046835 -0.143523874 -0.14818254  0.017971560 -0.0570694427
[10,]  0.018075323 -0.071978147 -0.05505238 -0.015669843 -0.0429070503
[11,]  0.005702599  0.007854398 -0.05601202  0.016231803 -0.1300416778
[12,] -0.006558771 -0.003838579 -0.22468384  0.038659604 -0.0006420412
[13,] -0.007572185 -0.076673340 -0.09814571  0.003114490 -0.0063303875
[14,] -0.280498809 -0.163002858 -0.13036331  0.007299739  0.0816608251
[15,] -0.237434576  0.098901652 -0.15528065 -0.030112853 -0.0027945345
[16,]  0.106908934 -0.135526965 -0.21137368 -0.017882215 -0.0563171287
[17,]  0.151601324 -0.020970597 -0.27196326  0.115372548  0.0255055220
[18,] -0.054581593  0.056964806 -0.08970450  0.098860738  0.0313524168
[19,] -0.056192999  0.173460307  0.03310134  0.084845172  0.0266576213
[20,]  0.048104195  0.206067418 -0.08665860  0.071857846  0.0065495692
[21,]  0.008294344  0.206957815  0.03099108  0.082836322 -0.0135914784
[22,]  0.035030061  0.254497792 -0.04721794  0.013091064  0.1689666861
[23,] -0.021333569  0.311371736 -0.08000049  0.029703757  0.0592841811
[24,] -0.025363775  0.329279517  0.17826476  0.121514740 -0.0261012964
[25,] -0.276846850  0.388971193 -0.08619670  0.139424447  0.0187128269
[26,] -0.144915814  0.133568407 -0.30209955  0.168305312 -0.1040820615
             [,11]         [,12]        [,13]
 [1,] -0.051736097 -0.0012347135  0.109718148
 [2,] -0.054729755 -0.0029547926  0.120352629
 [3,] -0.031043953  0.0086891955 -0.001536094
 [4,] -0.011784892  0.0355508274  0.028391616
 [5,]  0.023207027 -0.0259388357  0.004979915
 [6,] -0.044599707  0.0398585373  0.005601101
 [7,] -0.001947338 -0.0321964524  0.010246641
 [8,]  0.002016874 -0.0307550138 -0.007038728
 [9,] -0.100015788 -0.0139119705  0.031769093
[10,] -0.029580682  0.0019529048  0.046024156
[11,] -0.018201155  0.0007787248  0.035475400
[12,]  0.032620334  0.0160398191  0.057289831
[13,] -0.003395825  0.0297345280  0.017334526
[14,] -0.018428357 -0.0477625414 -0.013055988
[15,] -0.009387671  0.0498939299  0.004388167
[16,]  0.012749378 -0.0010003452 -0.041148781
[17,] -0.005488661 -0.0037005913 -0.051744968
[18,]  0.051072844 -0.0025081453 -0.006252586
[19,]  0.011402396 -0.0985193728 -0.017694929
[20,]  0.005328827 -0.0708702079 -0.015305694
[21,]  0.031211959 -0.0308361958  0.012185155
[22,]  0.046301713 -0.0214031807  0.013378959
[23,]  0.065770407  0.0021263322  0.027935761
[24,]  0.056454285 -0.0639773737  0.028255696
[25,]  0.086654753 -0.0338280515 -0.078252633
[26,]  0.093777690 -0.0372940208 -0.021628844

$ar.max
[1] 26

$ma.ma
[1] 13

$symbol
   0   1   2   3   4   5   6   7   8   9   10  11  12 
0  "x" "o" "o" "o" "o" "o" "x" "x" "o" "o" "o" "o" "o"
1  "x" "x" "o" "o" "o" "o" "x" "o" "o" "o" "o" "o" "o"
2  "o" "o" "x" "o" "o" "o" "x" "o" "o" "o" "o" "o" "o"
3  "x" "o" "x" "o" "o" "o" "x" "o" "o" "o" "o" "o" "o"
4  "o" "o" "o" "o" "o" "o" "x" "o" "o" "o" "o" "o" "o"
5  "x" "x" "o" "o" "o" "o" "x" "o" "o" "o" "o" "o" "o"
6  "o" "x" "o" "o" "x" "x" "x" "o" "o" "o" "o" "o" "o"
7  "o" "x" "o" "o" "x" "x" "o" "o" "o" "o" "o" "o" "o"
8  "x" "x" "x" "o" "x" "x" "x" "x" "o" "o" "o" "o" "o"
9  "o" "x" "o" "o" "x" "o" "o" "o" "o" "o" "o" "o" "o"
10 "o" "x" "x" "o" "o" "o" "o" "o" "o" "o" "o" "o" "o"
11 "x" "x" "x" "x" "o" "o" "o" "x" "o" "o" "o" "o" "o"
12 "x" "x" "x" "x" "o" "o" "o" "o" "o" "o" "o" "o" "o"
13 "x" "o" "x" "o" "o" "x" "x" "o" "o" "o" "o" "o" "o"
14 "x" "x" "x" "o" "o" "x" "o" "x" "o" "o" "o" "o" "o"
15 "x" "o" "x" "o" "x" "o" "x" "x" "o" "o" "o" "o" "o"
16 "x" "o" "x" "o" "o" "x" "o" "x" "o" "o" "o" "o" "o"
17 "x" "x" "x" "x" "o" "o" "o" "o" "o" "o" "o" "o" "o"
18 "x" "x" "o" "x" "o" "o" "x" "o" "o" "o" "o" "o" "o"
19 "x" "x" "o" "x" "o" "o" "x" "o" "o" "o" "o" "o" "o"
20 "o" "x" "o" "o" "o" "o" "x" "o" "o" "o" "o" "o" "o"
21 "o" "o" "x" "o" "o" "o" "x" "o" "o" "x" "o" "o" "o"
22 "x" "x" "o" "o" "x" "o" "x" "o" "o" "o" "o" "o" "o"
23 "x" "o" "o" "x" "x" "o" "x" "x" "o" "o" "o" "o" "o"
24 "x" "x" "x" "o" "o" "x" "x" "o" "x" "o" "o" "o" "o"
25 "x" "x" "o" "o" "o" "x" "o" "x" "x" "o" "o" "o" "o"

$`0`
 [1] "-0.3323" "-0.4488" "-0.0628" "-0.4893" "-0.1195" "-0.2041" "-0.0651"
 [8] "-0.0875" "-0.1608" " 0.0176" " 0.0312" "-0.4877" " 0.4583" " 0.2225"
[15] "-0.1809" " 0.4569" " 0.3528" "-0.3268" " 0.2028" "-0.3861" " 0.0186"
[22] " 0.0452" " 0.3942" " 0.5005" "-0.3965" "-0.2197"

$`1`
 [1] "-0.064254" "-0.281984" "-0.061023" " 0.000724" "-0.105819" "-0.298158"
 [7] "-0.286178" "-0.397286" "-0.385927" "-0.459485" "-0.250678" " 0.311917"
[13] " 0.160091" " 0.130742" " 0.387263" "-0.107219" "-0.051568" " 0.288669"
[19] " 0.265537" " 0.289993" "-0.348929" "-0.090343" " 0.182563" " 0.121115"
[25] " 0.368508" " 0.281254"

$`2`
 [1] " 0.0742" " 0.0612" "-0.1803" "-0.2533" "-0.0293" "-0.0427" "-0.0128"
 [8] " 0.0726" "-0.1664" " 0.0872" "-0.3259" "-0.3816" "-0.2967" "-0.3532"
[15] "-0.3003" "-0.2993" "-0.3720" "-0.2296" " 0.1001" "-0.0771" " 0.0713"
[22] " 0.2195" "-0.0151" "-0.0207" "-0.1879" "-0.0615"

$`3`
 [1] "-0.02307" "-0.01690" "-0.03125" " 0.06140" " 0.01066" " 0.00602"
 [7] "-0.04064" "-0.12508" "-0.03544" "-0.07931" "-0.12090" "-0.18384"
[13] "-0.19943" "-0.08096" "-0.06807" "-0.11328" "-0.12100" " 0.17717"
[19] " 0.19180" " 0.15601" " 0.11378" " 0.02273" "-0.04469" "-0.16626"
[25] " 0.07636" " 0.07482"

$`4`
 [1] "-0.07446" "-0.08694" "-0.01887" "-0.01071" "-0.08406" "-0.04809"
 [7] "-0.38405" "-0.34040" "-0.21080" "-0.19021" " 0.02498" " 0.11310"
[13] " 0.04036" "-0.00223" " 0.00393" " 0.23281" " 0.10925" "-0.05106"
[19] "-0.03448" " 0.04076" "-0.05971" "-0.13161" "-0.30377" "-0.30765"
[25] " 0.07359" " 0.06358"

$`5`
 [1] " 0.03919" "-0.02063" "-0.00792" "-0.01576" "-0.01407" "-0.01331"
 [7] "-0.34808" " 0.26982" " 0.27805" " 0.01808" " 0.00570" "-0.00656"
[13] "-0.00757" "-0.28050" "-0.23743" " 0.10691" " 0.15160" "-0.05458"
[19] "-0.05619" " 0.04810" " 0.00829" " 0.03503" "-0.02133" "-0.02536"
[25] "-0.27685" "-0.14492"

$`6`
 [1] "-0.21311" "-0.17381" "-0.19194" "-0.16926" "-0.17311" "-0.19219"
 [7] "-0.16193" "-0.11468" "-0.14352" "-0.07198" " 0.00785" "-0.00384"
[13] "-0.07667" "-0.16300" " 0.09890" "-0.13553" "-0.02097" " 0.05696"
[19] " 0.17346" " 0.20607" " 0.20696" " 0.25450" " 0.31137" " 0.32928"
[25] " 0.38897" " 0.13357"

$`7`
 [1] " 0.1550" " 0.0355" " 0.0533" "-0.0614" " 0.0438" "-0.0893" " 0.0222"
 [8] "-0.1297" "-0.1482" "-0.0551" "-0.0560" "-0.2247" "-0.0981" "-0.1304"
[15] "-0.1553" "-0.2114" "-0.2720" "-0.0897" " 0.0331" "-0.0867" " 0.0310"
[22] "-0.0472" "-0.0800" " 0.1783" "-0.0862" "-0.3021"

$`8`
 [1] "-0.09100" " 0.00660" " 0.00255" "-0.04459" "-0.01788" "-0.06020"
 [7] "-0.03731" "-0.03475" " 0.01797" "-0.01567" " 0.01623" " 0.03866"
[13] " 0.00311" " 0.00730" "-0.03011" "-0.01788" " 0.11537" " 0.09886"
[19] " 0.08485" " 0.07186" " 0.08284" " 0.01309" " 0.02970" " 0.12151"
[25] " 0.13942" " 0.16831"

$`9`
 [1] " 0.061637" "-0.016491" "-0.003969" " 0.001634" "-0.019203" "-0.035562"
 [7] " 0.041164" "-0.030437" "-0.057069" "-0.042907" "-0.130042" "-0.000642"
[13] "-0.006330" " 0.081661" "-0.002795" "-0.056317" " 0.025506" " 0.031352"
[19] " 0.026658" " 0.006550" "-0.013591" " 0.168967" " 0.059284" "-0.026101"
[25] " 0.018713" "-0.104082"

$`10`
 [1] "-0.05174" "-0.05473" "-0.03104" "-0.01178" " 0.02321" "-0.04460"
 [7] "-0.00195" " 0.00202" "-0.10002" "-0.02958" "-0.01820" " 0.03262"
[13] "-0.00340" "-0.01843" "-0.00939" " 0.01275" "-0.00549" " 0.05107"
[19] " 0.01140" " 0.00533" " 0.03121" " 0.04630" " 0.06577" " 0.05645"
[25] " 0.08665" " 0.09378"

$`11`
 [1] "-0.001235" "-0.002955" " 0.008689" " 0.035551" "-0.025939" " 0.039859"
 [7] "-0.032196" "-0.030755" "-0.013912" " 0.001953" " 0.000779" " 0.016040"
[13] " 0.029735" "-0.047763" " 0.049894" "-0.001000" "-0.003701" "-0.002508"
[19] "-0.098519" "-0.070870" "-0.030836" "-0.021403" " 0.002126" "-0.063977"
[25] "-0.033828" "-0.037294"

$`12`
 [1] " 0.10972" " 0.12035" "-0.00154" " 0.02839" " 0.00498" " 0.00560"
 [7] " 0.01025" "-0.00704" " 0.03177" " 0.04602" " 0.03548" " 0.05729"
[13] " 0.01733" "-0.01306" " 0.00439" "-0.04115" "-0.05174" "-0.00625"
[19] "-0.01769" "-0.01531" " 0.01219" " 0.01338" " 0.02794" " 0.02826"
[25] "-0.07825" "-0.02163"
In [313]:
cat("order =", ar(diff(diff_lg_gdpdef), method = 'mle')$order, "\n")
order = 12 
In [319]:
gof = 24
In [320]:
gdpdef_m1 = arima(lg_gdpdef, order = c(4, 2, 3), method = "ML")
summary(gdpdef_m1)
tsdiag1(gdpdef_m1, gof.lag = gof)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_gdpdef, order = c(4, 2, 3), method = "ML")

Coefficients:
          ar1     ar2     ar3     ar4     ma1      ma2      ma3
      -1.0971  0.0594  0.6949  0.2611  0.6690  -0.6601  -0.7676
s.e.   0.1328  0.1526  0.1331  0.0768  0.1259   0.0934   0.1283

sigma^2 estimated as 1.694e-05:  log likelihood = 1001.71,  aic = -1989.42

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
No description has been provided for this image
In [322]:
gdpdef_m2 = arima(
    lg_gdpdef, 
    fixed = c(NA, 0, NA, NA, NA, NA, NA),
    order = c(4, 2, 3), method = "ML")
summary(gdpdef_m2)
tsdiag1(gdpdef_m2, gof.lag = gof);
lrtest(gdpdef_m1, gdpdef_m2)
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“some AR parameters were fixed: setting transform.pars = FALSE”
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_gdpdef, order = c(4, 2, 3), fixed = c(NA, 0, NA, NA, NA, NA, NA), 
    method = "ML")

Coefficients:
          ar1  ar2     ar3     ar4     ma1      ma2      ma3
      -1.1339    0  0.6695  0.2474  0.6954  -0.6325  -0.7789
s.e.   0.0963    0  0.1058  0.0686  0.1057   0.0549   0.1035

sigma^2 estimated as 1.695e-05:  log likelihood = 1001.63,  aic = -1991.26

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
181001.710NA NA NA
271001.631-10.15930050.6898013
No description has been provided for this image
In [323]:
gdpdef_m3 = arima(
    lg_gdpdef, 
    order = c(6, 2, 5), method = "ML")
summary(gdpdef_m3)
tsdiag1(gdpdef_m3, gof.lag = gof);
lrtest(gdpdef_m1, gdpdef_m3)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_gdpdef, order = c(6, 2, 5), method = "ML")

Coefficients:
          ar1     ar2      ar3      ar4     ar5     ar6      ma1      ma2
      -0.0466  0.8607  -0.0024  -0.6976  0.0512  0.0144  -0.3956  -0.9939
s.e.   0.4670  0.2206   0.5382   0.3543  0.2724  0.1829   0.4644   0.1228
         ma3     ma4      ma5
      0.4322  0.7178  -0.5606
s.e.  0.6130  0.1629   0.3644

sigma^2 estimated as 1.642e-05:  log likelihood = 1004.99,  aic = -1987.98

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
1 81001.710NA NA NA
2121004.992 46.5631630.1608539
No description has been provided for this image
In [325]:
gdpdef_fm <- auto.arima(
    lg_gdpdef,
    d = NA,
    D = 0,
    max.p = 6,
    max.d = 3,
    max.q = 6,
    max.P = 0,
    max.Q = 0,
    max.order = 12,
    seasonal = TRUE,
    method = "ML",
    allowmean = TRUE,
    stepwise = FALSE,
    parallel = TRUE,
    num.cores = 12
)
summary(gdpdef_fm)
Series: lg_gdpdef 
ARIMA(2,2,5) 

Coefficients:
          ar1     ar2      ma1      ma2     ma3    ma4      ma5
      -0.3799  0.4340  -0.0566  -0.7254  0.2396  0.027  -0.2686
s.e.   0.1653  0.1797   0.1635   0.1396  0.1182  0.071   0.0729

sigma^2 = 1.741e-05:  log likelihood = 1002.36
AIC=-1988.72   AICc=-1988.11   BIC=-1960.68

Training set error measures:
                       ME        RMSE         MAE          MPE       MAPE
Training set -0.000151253 0.004096017 0.002780165 -0.004024708 0.08141952
                  MASE         ACF1
Training set 0.3195866 -0.003734142
In [327]:
gdpdef_m4 = arima(
    lg_gdpdef, 
    order = c(2, 2, 5), method = "ML")
summary(gdpdef_m4)
tsdiag1(gdpdef_m4, gof.lag = gof);
lrtest(gdpdef_m1, gdpdef_m4)
lrtest(gdpdef_m3, gdpdef_m4)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_gdpdef, order = c(2, 2, 5), method = "ML")

Coefficients:
          ar1     ar2      ma1      ma2     ma3    ma4      ma5
      -0.3799  0.4340  -0.0566  -0.7254  0.2396  0.027  -0.2686
s.e.   0.1653  0.1797   0.1635   0.1396  0.1182  0.071   0.0729

sigma^2 estimated as 1.685e-05:  log likelihood = 1002.36,  aic = -1990.72

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
181001.71NA NANA
281002.36 01.299554 0
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
1121004.992NA NA NA
2 81002.360-45.2636090.261303
No description has been provided for this image
In [328]:
gdpdef_m5 = arima(
    lg_gdpdef, 
    fixed = c(NA, NA, 0, NA, NA, 0, NA),
    order = c(2, 2, 5), method = "ML")
summary(gdpdef_m5)
tsdiag1(gdpdef_m5, gof.lag = gof);
lrtest(gdpdef_m1, gdpdef_m5)
lrtest(gdpdef_m3, gdpdef_m5)
lrtest(gdpdef_m4, gdpdef_m5)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_gdpdef, order = c(2, 2, 5), fixed = c(NA, NA, 0, NA, NA, 0, NA), 
    method = "ML")

Coefficients:
          ar1     ar2  ma1      ma2     ma3  ma4      ma5
      -0.4371  0.3975    0  -0.7054  0.2111    0  -0.2658
s.e.   0.0618  0.1553    0   0.1393  0.0896    0   0.0726

sigma^2 estimated as 1.687e-05:  log likelihood = 1002.27,  aic = -1994.54

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
181001.71NA NA NA
261002.27-21.1186790.5715863
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
1121004.992NA NA NA
2 61002.270-65.4444830.4881919
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
181002.36NA NA NA
261002.27-20.18087450.9135317
No description has been provided for this image
In [352]:
gdpdef_m6 = arima(
    lg_gdpdef, 
    order = c(4, 2, 5), method = "ML")
summary(gdpdef_m6)
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_gdpdef, order = c(4, 2, 5), method = "ML")

Coefficients:
          ar1     ar2      ar3      ar4      ma1      ma2     ma3     ma4
      -0.0355  0.8702  -0.1843  -0.4508  -0.4030  -1.0099  0.6201  0.4255
s.e.   0.2271  0.2393   0.1969   0.1775   0.2204   0.2135  0.2618  0.1737
          ma5
      -0.4403
s.e.   0.0864

sigma^2 estimated as 1.661e-05:  log likelihood = 1004.11,  aic = -1990.23

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
In [354]:
gdpdef_m7 = arima(
    lg_gdpdef, 
    fixed = c(0, NA, 0, NA, NA, NA, NA, NA, NA),
    order = c(4, 2, 5), method = "ML")
summary(gdpdef_m7)
tsdiag1(gdpdef_m5, gof.lag = gof);
lrtest(gdpdef_m5, gdpdef_m7)
Warning message in stats::arima(x = x, order = order, seasonal = seasonal, xreg = xreg, :
“some AR parameters were fixed: setting transform.pars = FALSE”
Warning message in trainingaccuracy(object, test, d, D):
“test elements must be within sample”
Call:
arima(x = lg_gdpdef, order = c(4, 2, 5), fixed = c(0, NA, 0, NA, NA, NA, NA, 
    NA, NA), method = "ML")

Coefficients:
      ar1     ar2  ar3      ar4      ma1      ma2     ma3     ma4      ma5
        0  0.8479    0  -0.6614  -0.7872  -1.1169  0.6667  0.7227  -0.7267
s.e.    0  0.2179    0   0.2038   0.1660   0.2247  0.2414  0.2699   0.2024

sigma^2 estimated as 1.167e-05:  log likelihood = 1004.75,  aic = -1995.5

Training set error measures:
              ME RMSE MAE MPE MAPE
Training set NaN  NaN NaN NaN  NaN
A anova: 2 × 5
#DfLogLikDfChisqPr(>Chisq)
<dbl><dbl><dbl><dbl><dbl>
161002.270NA NA NA
281004.751 24.9626820.08363101
No description has been provided for this image
In [ ]:

Forecast

In [573]:
npts = 8
eotr = length(lg_gdpdef_ts)-npts
h = npts
freq = 4
gdpdef_fc_res = plot_auto_arima_forecast_fig(
    da_ts=lg_gdpdef_ts, eotr=eotr, h=h, npts=npts, frequency=freq,
    main="Forecasts from ARIMA(4,2,5)\nfor GDP Deflator",
    xlab="Year", ylab="lg(GDP Deflator)", ylim=c(4.7, 4.95),
    d = NA,
    D = 0,
    max.p = 6,
    max.d = 3,
    max.q = 6,
    max.P = 0,
    max.Q = 0,
    max.order = 12,
    seasonal = TRUE,
    method = "ML",
    allowmean = TRUE,
    stepwise = FALSE,
    parallel = TRUE,
    num.cores = 4
)
summary(gdpdef_fc_res)
[1] 238
2004.75 ; 2009
No description has been provided for this image
Forecast method: ARIMA(4,2,5)

Model Information:
Series: tr_da_ts 
ARIMA(4,2,5) 

Coefficients:
          ar1     ar2     ar3      ar4      ma1      ma2     ma3     ma4
      -0.0718  0.8655  0.0615  -0.6654  -0.3640  -1.0088  0.3642  0.7045
s.e.   0.1688  0.1651  0.1428   0.1480   0.1669   0.1441  0.1846  0.1530
          ma5
      -0.4941
s.e.   0.0909

sigma^2 = 1.733e-05:  log likelihood = 971.05
AIC=-1922.1   AICc=-1921.13   BIC=-1887.37

Error measures:
                       ME        RMSE         MAE          MPE      MAPE
Training set -0.000116935 0.004066397 0.002832421 -0.003132418 0.0833439
                   MASE        ACF1
Training set 0.08156491 0.003210161

Forecasts:
        Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
2007 Q1       4.773491 4.768156 4.778826 4.765332 4.781650
2007 Q2       4.779424 4.769519 4.789328 4.764276 4.794571
2007 Q3       4.785139 4.770517 4.799762 4.762776 4.807503
2007 Q4       4.790914 4.771031 4.810796 4.760507 4.821321
2008 Q1       4.797842 4.772501 4.823183 4.759086 4.836597
2008 Q2       4.804157 4.773574 4.834740 4.757384 4.850929
2008 Q3       4.811662 4.775869 4.847456 4.756920 4.866404
2008 Q4       4.818584 4.778003 4.859165 4.756521 4.880647
No description has been provided for this image
In [634]:
gdpdef_fc_tb = comb_forecast_res(gdpdef_fc_res, lg_gdpdef_ts, eotr, freq)
gdpdef_fc_tb
Forecast method: ARIMA(4,2,5)

Model Information:
Series: tr_da_ts 
ARIMA(4,2,5) 

Coefficients:
          ar1     ar2     ar3      ar4      ma1      ma2     ma3     ma4
      -0.0718  0.8655  0.0615  -0.6654  -0.3640  -1.0088  0.3642  0.7045
s.e.   0.1688  0.1651  0.1428   0.1480   0.1669   0.1441  0.1846  0.1530
          ma5
      -0.4941
s.e.   0.0909

sigma^2 = 1.733e-05:  log likelihood = 971.05
AIC=-1922.1   AICc=-1921.13   BIC=-1887.37

Error measures:
                       ME        RMSE         MAE          MPE      MAPE
Training set -0.000116935 0.004066397 0.002832421 -0.003132418 0.0833439
                   MASE        ACF1
Training set 0.08156491 0.003210161

Forecasts:
        Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
2007 Q1       4.773491 4.768156 4.778826 4.765332 4.781650
2007 Q2       4.779424 4.769519 4.789328 4.764276 4.794571
2007 Q3       4.785139 4.770517 4.799762 4.762776 4.807503
2007 Q4       4.790914 4.771031 4.810796 4.760507 4.821321
2008 Q1       4.797842 4.772501 4.823183 4.759086 4.836597
2008 Q2       4.804157 4.773574 4.834740 4.757384 4.850929
2008 Q3       4.811662 4.775869 4.847456 4.756920 4.866404
2008 Q4       4.818584 4.778003 4.859165 4.756521 4.880647
A Time Series: 2 × 4
Qtr1Qtr2Qtr3Qtr4
20074.7734914.7794244.7851394.790914
20084.7978424.8041574.8116624.818584
A Time Series: 2 × 4
Qtr1Qtr2Qtr3Qtr4
20070.0041629190.0077285790.0114101370.015514136
20080.0197736620.0238640590.0279300660.031665186
A Time Series: 2 × 4
Qtr1Qtr2Qtr3Qtr4
20074.7787544.7837104.7874674.793664
20084.7999804.8031194.8126404.814166
A Time Series: 8 × 3
ForecastStd. ErrorActual
2007 Q14.7734910.0041629194.778754
2007 Q24.7794240.0077285794.783710
2007 Q34.7851390.0114101374.787467
2007 Q44.7909140.0155141364.793664
2008 Q14.7978420.0197736624.799980
2008 Q24.8041570.0238640594.803119
2008 Q34.8116620.0279300664.812640
2008 Q44.8185840.0316651864.814166
In [550]:
gdpdef_fc_res1 = plot_auto_arima_forecast_fig(
    da_ts=lg_gdpdef_ts, eotr=length(lg_gdpdef_ts), h=h, npts=npts, frequency=freq,
    main="Forecasts from ARIMA(2,2,5)\nfor GDP Deflator",
    xlab="Year", ylab="lg(GDP Deflator)", ylim=c(4.7, 4.95),
    d = NA,
    D = 0,
    max.p = 6,
    max.d = 3,
    max.q = 6,
    max.P = 0,
    max.Q = 0,
    max.order = 12,
    seasonal = TRUE,
    method = "ML",
    allowmean = TRUE,
    stepwise = FALSE,
    parallel = TRUE,
    num.cores = 4
)
summary(gdpdef_fc_res1)
[1] 246
2006.75 ; 2011
No description has been provided for this image
Forecast method: ARIMA(2,2,5)

Model Information:
Series: tr_da_ts 
ARIMA(2,2,5) 

Coefficients:
          ar1     ar2      ma1      ma2     ma3    ma4      ma5
      -0.3799  0.4340  -0.0566  -0.7254  0.2396  0.027  -0.2686
s.e.   0.1653  0.1797   0.1635   0.1396  0.1182  0.071   0.0729

sigma^2 = 1.741e-05:  log likelihood = 1002.36
AIC=-1988.72   AICc=-1988.11   BIC=-1960.68

Error measures:
                       ME        RMSE         MAE          MPE       MAPE
Training set -0.000151253 0.004096017 0.002780165 -0.004024708 0.08141952
                   MASE         ACF1
Training set 0.08087405 -0.003734142

Forecasts:
        Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
2009 Q1       4.818212 4.812865 4.823559 4.810034 4.826390
2009 Q2       4.823084 4.813160 4.833008 4.807906 4.838261
2009 Q3       4.827888 4.813292 4.842483 4.805566 4.850209
2009 Q4       4.831808 4.811881 4.851735 4.801333 4.862283
2010 Q1       4.837631 4.812022 4.863240 4.798465 4.876797
2010 Q2       4.842348 4.811315 4.873381 4.794887 4.889809
2010 Q3       4.848310 4.811804 4.884817 4.792478 4.904143
2010 Q4       4.853320 4.811518 4.895122 4.789390 4.917250
No description has been provided for this image
In [ ]:

In [551]:
order = c(2,2,5)
normal_gdpdef_fc_res = plot_arima_forecast_fig(
    da_ts=lg_gdpdef_ts, eotr=eotr, h=h, npts=npts, frequency=freq,
    main=NULL, #"Forecasts from ARIMA(4,2,5)\nfor GDP Deflator",
    xlab="Year", ylab="lg(GDP Deflator)", ylim=c(4.7, 4.95),
    order=order, method='ML'
)
[1] 238
2004.75 ; 2009
No description has been provided for this image
No description has been provided for this image
In [635]:
normal_gdpdef_fc_tb = comb_forecast_res(normal_gdpdef_fc_res, lg_gdpdef_ts, eotr, freq)
normal_gdpdef_fc_tb
'The "summary(forecast_obj)" call output length 5529 exceeds the output limit 5000 so that it is suppressed.'
A Time Series: 2 × 4
Qtr1Qtr2Qtr3Qtr4
20074.7743984.7798424.7859574.791801
20084.7983084.8044574.8110214.817279
A Time Series: 2 × 4
Qtr1Qtr2Qtr3Qtr4
20070.0041977210.0078259350.0115367580.015766736
20080.0202642490.0245652770.0288986530.033098071
A Time Series: 2 × 4
Qtr1Qtr2Qtr3Qtr4
20074.7787544.7837104.7874674.793664
20084.7999804.8031194.8126404.814166
A Time Series: 8 × 3
ForecastStd. ErrorActual
2007 Q14.7743980.0041977214.778754
2007 Q24.7798420.0078259354.783710
2007 Q34.7859570.0115367584.787467
2007 Q44.7918010.0157667364.793664
2008 Q14.7983080.0202642494.799980
2008 Q24.8044570.0245652774.803119
2008 Q34.8110210.0288986534.812640
2008 Q44.8172790.0330980714.814166
In [585]:
order = c(2,2,5)
normal_gdpdef_fc_res1 = plot_arima_forecast_fig(
    da_ts=lg_gdpdef_ts, eotr=length(lg_gdpdef_ts), h=h, npts=npts, frequency=freq,
    main="Forecasts from ARIMA(2,2,5)\nfor GDP Deflator",
    xlab="Year", ylab="lg(GDP Deflator)", ylim=c(4.7, 4.95),
    order=order, method='ML'
)
[1] 246
2006.75 ; 2011
No description has been provided for this image
No description has been provided for this image
In [616]:
comb_forecast_res(normal_gdpdef_fc_res1, lg_gdpdef_ts, length(lg_gdpdef_ts), freq)
'The "summary(forecast_obj)" call output length 5669 exceeds the output limit 5000 so that it is suppressed.'
A Time Series: 2 × 4
Qtr1Qtr2Qtr3Qtr4
20094.8182124.8230844.8278884.831808
20104.8376314.8423484.8483104.853320
A Time Series: 2 × 4
Qtr1Qtr2Qtr3Qtr4
20090.0041724260.0077438080.0113886510.015548946
20100.0199829870.0242150670.0284864710.032618017
A Time Series: 8 × 2
ForecastStd. Error
2009 Q14.8182120.004172426
2009 Q24.8230840.007743808
2009 Q34.8278880.011388651
2009 Q44.8318080.015548946
2010 Q14.8376310.019982987
2010 Q24.8423480.024215067
2010 Q34.8483100.028486471
2010 Q44.8533200.032618017
In [589]:
gdpdef_fc_res$model$coef; gdpdef_fc_res$mean;
normal_gdpdef_fc_res$model$coef; normal_gdpdef_fc_res$mean
ar1
-0.0717970949759296
ar2
0.86554096509274
ar3
0.0614636503852643
ar4
-0.665441530470489
ma1
-0.36400885162938
ma2
-1.00882887436828
ma3
0.364213542716386
ma4
0.70448947357745
ma5
-0.49409905850808
A Time Series: 2 × 4
Qtr1Qtr2Qtr3Qtr4
20074.7734914.7794244.7851394.790914
20084.7978424.8041574.8116624.818584
ar1
-0.365994578588744
ar2
0.429438668487666
ma1
-0.0605618077237294
ma2
-0.713126403818853
ma3
0.231445080760435
ma4
0.0211322968836547
ma5
-0.262889045155037
A Time Series: 2 × 4
Qtr1Qtr2Qtr3Qtr4
20074.7743984.7798424.7859574.791801
20084.7983084.8044574.8110214.817279
In [ ]:

Alternative solution

In [540]:
require(xts)

da = read.table("../AFTS_sol/data/q-gdpdef.txt", header = T)
y = log(da$gdpdef)
plot(xts(y, order.by = as.Date(paste(da$year, da$mom, da$day, sep = '-'))),
    type = 'o', main = '', xlab = 'date', ylab = 'y')
par(mfrow = c(2, 1), bg = 'white')
pacf(diff(y))
acf(diff(y))
No description has been provided for this image
No description has been provided for this image
In [539]:
par(bg = 'white')
m1 = arima(diff(y), order = c(2, 0, 1))
m1
tsdiag(m1, gof = 20)
m2 = arima(diff(y), order = c(2, 0, 1), seasonal = list(order = c(0, 0, 1), period = 7))
m2
tsdiag(m2, gof = 20)
mp = predict(m2, 4)
Y1 = y[length(y)] + mp$pred[1]
Y2 = Y1 + mp$pred[2]
Y3 = Y2 + mp$pred[3]
Y4 = Y3 + mp$pred[4]
cat("prediction =", Y1, Y2, Y3, Y4, "\n")
Call:
arima(x = diff(y), order = c(2, 0, 1))

Coefficients:
         ar1     ar2      ma1  intercept
      0.8768  0.0112  -0.3375     0.0086
s.e.  0.2024  0.1636   0.1923     0.0015

sigma^2 estimated as 1.764e-05:  log likelihood = 1000.8,  aic = -1993.6
Call:
arima(x = diff(y), order = c(2, 0, 1), seasonal = list(order = c(0, 0, 1), period = 7))

Coefficients:
         ar1      ar2      ma1     sma1  intercept
      1.2451  -0.2699  -0.6887  -0.2961     0.0084
s.e.  0.1759   0.1609   0.1483   0.0675     0.0021

sigma^2 estimated as 1.66e-05:  log likelihood = 1008.01,  aic = -2006.02
No description has been provided for this image
prediction = 4.818959 4.824743 4.830128 4.835893 
No description has been provided for this image
In [569]:
npts = 8
h = npts
freq = 4
order = c(2,1,1)
seas = list(order = c(0,0,1), period = 7)
alt_normal_gdpdef_fc_res = plot_arima_forecast_fig(
    da_ts=lg_gdpdef_ts, eotr=length(lg_gdpdef_ts), h=h, npts=npts, frequency=freq,
    main=NULL, #"Forecasts from ARIMA(4,2,5)\nfor GDP Deflator",
    xlab="Year", ylab="lg(GDP Deflator)", ylim=c(4.7, 4.95),
    order=order, seasonal=seas, method='ML', include.drift=T 
)
[1] 247
2006.75 ; 2011
No description has been provided for this image
No description has been provided for this image
In [571]:
alt_normal_gdpdef_fc_res$model;
m2;
alt_normal_gdpdef_fc_res$mean
Series: structure(c(2.71502581483864, 2.72974645918397, 2.74707858806969,  2.77190098580328, 2.77950226850885, 2.78833903233101, 2.80674857388649,  2.80922195613858, 2.80523740607088, 2.79422789734326, 2.78846207139607,  2.7894458393062, 2.78636834564815, 2.79030584304494, 2.81120829320484,  2.83026783382646, 2.86687565142899, 2.87299950817169, 2.87356463957978,  2.88457722418783, 2.88362683976968, 2.88552670623297, 2.89696111145397,  2.89988194789716, 2.89988194789716, 2.9017512100831, 2.90558872370103,  2.90777499479485, 2.9109910450989, 2.91191578746088, 2.9137084380754,  2.91652671537302, 2.92133195097774, 2.92552437851049, 2.93295220543128,  2.943016915882, 2.95308569883252, 2.95886079877809, 2.9716444780249,  2.97547854452672, 2.98951297374007, 2.99633209362596, 3.00226091551669,  3.00236025984421, 3.01332658195494, 3.0164658399409, 3.02300691568887,  3.02800583410429, 3.03032691831849, 3.03032691831849, 3.03269081453952,  3.03687421688517, 3.04123131362521, 3.04471289577561, 3.04851445899296,  3.05145076231033, 3.05362375196977, 3.05574494346016, 3.05884790786807,  3.06222201482282, 3.06809944567989, 3.06958664212447, 3.07199862900744,  3.07514401297815, 3.07740442219802, 3.0793838460496, 3.08135935952143,  3.08881269657, 3.09176946175093, 3.09435514215907, 3.09828886187908,  3.10324048126928, 3.10830174215749, 3.11262602502549, 3.11631139651238,  3.12267324699514, 3.12916991178371, 3.13757899778223, 3.14806701985727,  3.15721316447609, 3.16175505737844, 3.16720351129353, 3.1770116207695,  3.1880454144756, 3.19891798552725, 3.20935060364313, 3.21919577367912,  3.23325199081271, 3.24360746108738, 3.25647984726557, 3.27078492960563,  3.28372679718782, 3.29790879215622, 3.31156438942212, 3.31951722298605,  3.33234735711495, 3.3472690396366, 3.36030594028608, 3.37036009584199,  3.37833808751607, 3.39387060627569, 3.39982978025485, 3.40949618447685,  3.42269791310816, 3.43463215774839, 3.44998754583159, 3.46929207212749,  3.48890296208126, 3.50783707881836, 3.53111391521587, 3.56007970672235,  3.58955623281915, 3.6122683517421, 3.62737632151989, 3.64578926418223,  3.66322825722841, 3.6742225647703, 3.6848714327164, 3.69845842889426,  3.71654325809641, 3.73280064117186, 3.74717194686545, 3.75926801783586,  3.78082032781229, 3.79560154241089, 3.8137705012297, 3.83020539020437,  3.85114710204314, 3.86861433501112, 3.89300327159089, 3.91432036447683,  3.93411712290121, 3.95498670486026, 3.97685523342963, 3.99930101353521,  4.02661874482584, 4.05208055628073, 4.0707005663077, 4.08834325702626,  4.10642134344755, 4.11993108409958, 4.13199352800686, 4.14601945046012,  4.15674016394707, 4.16492728034398, 4.17212317029385, 4.18230965095074,  4.18983654368132, 4.2022568194165, 4.21085271148199, 4.21881628973294,  4.22515350233351, 4.23635036207667, 4.24204591836945, 4.246178274237,  4.25261531431182, 4.2577664172932, 4.26289112233309, 4.26866194871886,  4.27517890254657, 4.28340723543778, 4.28884169494619, 4.29626447708468,  4.30349736450916, 4.31196622928962, 4.32148013480585, 4.33258688456918,  4.34005814985138, 4.35130966201182, 4.36085406944108, 4.36791503272923,  4.37481318015558, 4.38670318255778, 4.39828130875438, 4.40709722460231,  4.41456642624166, 4.42635447652338, 4.43277915787105, 4.43984732915758,  4.44507185389093, 4.45109783642627, 4.45655416165931, 4.46106884201383,  4.4662413964447, 4.47400171375275, 4.47949357790009, 4.48379319982149,  4.4890632449001, 4.49510975411777, 4.49929842855735, 4.50568128748735,  4.51033189482681, 4.51666678739669, 4.52025479270732, 4.52492495780866,  4.52972439627847, 4.53612017001128, 4.53966052678189, 4.54277336976238,  4.54807059394752, 4.55444515116312, 4.5560429650214, 4.55948220506867,  4.56274273674814, 4.56527484532484, 4.56693858350353, 4.57057874121847,  4.5740303345467, 4.57808671757835, 4.58163540285443, 4.58510012293416,  4.58936595444312, 4.59831675478708, 4.60261692920037, 4.60775683771819,  4.61180810616827, 4.61984202621996, 4.62744035463382, 4.63156865984262,  4.63658163994218, 4.64022840180315, 4.64379456752486, 4.6475397823546,  4.65307424339515, 4.66083192481299, 4.6640238287818, 4.66918669590151,  4.67460296070343, 4.68375028611095, 4.6929795778125, 4.6985967747663,  4.70656183610828, 4.7163984526738, 4.72158339497128, 4.73152085884338,  4.74079313977281, 4.74955620343901, 4.75631922253102, 4.76308804678459,  4.76841085496925, 4.77875367683876, 4.78370959948659, 4.78746674246954,  4.7936643198378, 4.79998010423441, 4.80311899866775, 4.8126395363147,  4.81416613019623), tsp = c(1947, 2008.75, 4), class = "ts") 
ARIMA(2,1,1)(0,0,1)[7] with drift 

Coefficients:
         ar1      ar2      ma1     sma1   drift
      1.2448  -0.2697  -0.6885  -0.2960  0.0084
s.e.  0.1760   0.1610   0.1484   0.0675  0.0021

sigma^2 = 1.697e-05:  log likelihood = 1008.01
AIC=-2004.02   AICc=-2003.67   BIC=-1982.96
Call:
arima(x = diff(y), order = c(2, 0, 1), seasonal = list(order = c(0, 0, 1), period = 7))

Coefficients:
         ar1      ar2      ma1     sma1  intercept
      1.2451  -0.2699  -0.6887  -0.2961     0.0084
s.e.  0.1759   0.1609   0.1483   0.0675     0.0021

sigma^2 estimated as 1.66e-05:  log likelihood = 1008.01,  aic = -2006.02
A Time Series: 2 × 4
Qtr1Qtr2Qtr3Qtr4
20094.8189604.8247444.8301304.835895
20104.8426564.8477684.8550224.861769
In [572]:
cat("prediction =", Y1, Y2, Y3, Y4, "\n")
prediction = 4.818959 4.824743 4.830128 4.835893 
In [ ]:

End

In [ ]:

In [ ]:

In [ ]: